tid=90325&當php 5.5以上 遇上 mysql 5.2 時,連接mysql的指令 mysqli_connect() 會產生錯誤mysqli_connect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password').
# c+ N' _& F7 ?5 b' s* J5 `- W3 m" s3 w+ l1 }* j" h6 z
這是因為php 5.3以前舊版的密碼採16位編碼,而新版php 5.3以後採41位編碼,而mysql 5.2 以前預設也是16碼,所以才會造成此種錯誤.
3 c6 H1 ^) ?+ k; |先診斷一下mysql: (我的php 5.6.38 , mysql 5.1.73)9 s& s& q, X" q# w7 U( g
登入mysql 然後輸入 :
! ]+ e2 E2 @1 B# X" Q* h9 hmysql> SHOW VARIABLES LIKE 'old_passwords';1 E# V9 @7 z2 _+ r2 j# l/ B9 ~3 m
+------------------+-------+% K( t. R$ t/ f
| Variable_name | Value |
( j& o d( y3 i3 D& p$ N+------------------+-------+ L0 q4 w' J) w: b5 c( j
| old_passwords | ON |: u, s1 W+ p' {) b3 Q+ N
+------------------+-------+
. C: w# T2 X5 Q8 G$ L; R1 row in set (0.00 sec)
; n6 N' w. `- f/ O- ^
! I, U: |2 ~8 pold_password ==> ON 就表示 /etc/my.cnf 裏的 old_passwords=1 設定為16碼,須將它設為 0 然後重啟mysqld ==> service mysqld restart
1 ]/ v2 j+ _( J# H9 i或 在 mysql prompt下輸入:) K$ O0 e4 B! B0 [. \, a
mysql> SET old_passwords=FALSE;
0 [- Y: t1 Y( c檢查mysql.user內 每個密碼長度:$ @2 e' o: W$ r( A- C9 y
mysql> SELECT 'User', 'Host', Length('Password') FROM mysql.user;* B; _' k" P( k9 N
如果還沒改成41位,Length('Password')這個欄位應該都是16或是0(表示沒設密碼)
( e. E7 R' M3 u/ {- v- ~* B
8 i9 ]( V/ A2 ~! ~) F% \' @再重設原來的密碼:: ]- o! G0 r0 R
mysql> SET PASSWORD FOR 'root'@'192.168.1.1' = PASSWORD('原來的密碼'); // 小心要核對原來 帳號@IP 更改,不要改錯了
$ Y/ U) k/ D y+ E) v+ |mysql> flush privileges;
' B r; ~2 r% E+ T( ~$ }' Y& t X/ ~" R/ h5 G& K' |
再輸入 SELECT 'User', 'Host', Length('Password') FROM mysql.user; 檢查密碼度,就可發現剛剛改的root 的密碼長度已改為41碼/ A g, S8 p, i* c4 l! r$ v# P0 h9 u
注意:, ~/ _! a; N( G7 @6 K# \
如果帳號太多,可以遇到問題時再重設密碼,因為重設密碼 SET PASSWORD FOR 只針對個別密碼,不會因為 old_passwords=0 而對所有密碼造成影響===========================================================================
h, D- i& X9 }7 Y; f8 S當mysql升級到 8.0.21時,php連到mysql出現2行errors:
1 E# L) b. h& i. `mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers
. a T: |5 A! I. v8 a) Cmysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers
2 {# _' n x9 Z. H! A7 A5 `原因:3 J7 m: e: Z3 r# K8 p3 B; E
在MySQL 8.0.21中,caching_sha2_password是默認的身份驗證插件,而不是以往的mysql_native_password。所以和php不相容。可以降級php,也可以修改MySQL的配置。 吾人決定修改MySQL的配置:6 w6 p4 {6 e3 h5 F# H( x
vi /etc/my.cnf 加入下列:3 E2 }# y3 V! d+ L% y3 c. d
[mysqld]
- ?5 K) o9 _7 S# A& l% d6 \ ^* U
8 M4 ]6 @# B6 V; G' ccharacter-set-server=utf8# Z, \+ t0 N8 m3 j
default_authentication_plugin=mysql_native_password
/ k- @/ A0 t& W3 X" r5 H1 r% ?& i2 p: o! A; I2 |9 ?
[mysql]' L1 |' D9 Y% [1 w$ l* Z
default-character-set=utf8( U) M4 m- \7 s' _# K [/ _: a
+ R) n9 r$ ]1 O; j+ o6 S
[client]( W" ?" {/ @3 D/ k" w9 U
default-character-set=utf8: Z" A. y# V7 {3 s) m9 e/ K( L9 A
2 u1 G3 m- F" n% U
然後重啟mysqld
) O! {$ p) J$ \8 z: J ~/ T% qservice mysqld restart
8 A0 c( r" A+ \2 ~5 ]9 M搞定!!
% k0 H+ @) S9 e0 p2 ?
/ r5 [$ `/ {3 Z
, d$ I, B& Y8 T. B) y' i! y+ h/ U; e: @ |