當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').1 i6 G* C5 G8 v% x3 j
/ D! w9 [- p6 z
這是因為php 5.3以前舊版的密碼採16位編碼,而新版php 5.3以後採41位編碼,而mysql 5.2 以前預設也是16碼,所以才會造成此種錯誤.
8 j' v2 N! X( `% {9 K# K! d/ d先診斷一下mysql: (我的php 5.6.38 , mysql 5.1.73)' V( Y, a: @, {9 ^) x0 }
登入mysql 然後輸入 : k4 V3 l# E" n& f E' }2 G
mysql> SHOW VARIABLES LIKE 'old_passwords';
- W* M6 X2 l/ y0 I. a8 q* |5 i6 p+------------------+-------+
# X; k `/ D' E% H" i* }| Variable_name | Value |# O7 O+ k) P# N6 K S4 Y
+------------------+-------+9 V9 L$ R/ ]9 B: S" k: I
| old_passwords | ON |- A1 q8 S6 y$ f( x' y) n( q$ n6 N
+------------------+-------+
+ [$ E$ ~/ L+ }2 t8 I7 w1 row in set (0.00 sec)
4 X3 i1 }5 a( }4 ^
`4 L8 ~* F' f" bold_password ==> ON 就表示 /etc/my.cnf 裏的 old_passwords=1 設定為16碼,須將它設為 0 然後重啟mysqld ==> service mysqld restart
; F/ s1 V% g! `或 在 mysql prompt下輸入:+ t) ]- ~5 B2 Y: i3 W
mysql> SET old_passwords=FALSE;
) }; F9 t e( w$ [2 [+ W u! t A" m8 s檢查mysql.user內 每個密碼長度:) D4 _" P5 @9 h! q: M" [3 k, D
mysql> SELECT 'User', 'Host', Length('Password') FROM mysql.user;
9 m$ P" d* s1 R1 Z1 R; O如果還沒改成41位,Length('Password')這個欄位應該都是16或是0(表示沒設密碼)
3 ^: f- D2 }, f. B _
2 _3 P3 I+ Z: M; v" i6 ^# B' |再重設原來的密碼:
* S9 x3 @, Y: i% g9 Y) M. r) gmysql> SET PASSWORD FOR 'root'@'192.168.1.1' = PASSWORD('原來的密碼'); // 小心要核對原來 帳號@IP 更改,不要改錯了
3 H U1 j7 ~$ r2 _% c$ zmysql> flush privileges;
1 a3 J* h& \0 }, t0 {) I3 G! g: b) J, Z( r0 @ c3 ~5 A' f
再輸入 SELECT 'User', 'Host', Length('Password') FROM mysql.user; 檢查密碼度,就可發現剛剛改的root 的密碼長度已改為41碼
0 {% _' A4 I* I注意:
! ~+ l! S7 i* ^# {6 ?; I如果帳號太多,可以遇到問題時再重設密碼,因為重設密碼 SET PASSWORD FOR 只針對個別密碼,不會因為 old_passwords=0 而對所有密碼造成影響===========================================================================
, _4 P. D/ C* I$ l! I當mysql升級到 8.0.21時,php連到mysql出現2行errors:
8 j' r: [- x8 _mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers4 c' H& p z! L' `+ ~: O
mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers% C) y& ?- D% s3 k6 U7 Z
原因:
* O8 Y, w8 U3 h在MySQL 8.0.21中,caching_sha2_password是默認的身份驗證插件,而不是以往的mysql_native_password。所以和php不相容。可以降級php,也可以修改MySQL的配置。 吾人決定修改MySQL的配置:
* e' N% W) N: N' m4 Z& Tvi /etc/my.cnf 加入下列:8 d: C. P, E! S- [5 }4 a& A6 _4 e
[mysqld]
6 @* C3 g& l( L0 }& O
% V1 l, a' C6 o0 j8 l9 z. ?character-set-server=utf8! K( @( g/ b4 ]$ r4 U! y
default_authentication_plugin=mysql_native_password1 _; m* c6 }$ Y( V
( ]. B, P/ _( z" G# l/ v1 i
[mysql]1 z& A4 L+ j) h! u4 f8 h& z0 z) B
default-character-set=utf8
( Y' F$ A6 h+ V9 y2 c
; W1 H5 r/ O0 k2 |[client]3 E, Z. x! x' i* N% |, ?
default-character-set=utf8+ x, w/ x l: x0 M7 k1 T$ f* t
4 R6 e7 D' A- B然後重啟mysqld2 x. c4 H4 w; O
service mysqld restart
1 L; \5 o7 {: x: q3 n, f4 x搞定!!
! j! f5 ]( {0 @
' t8 [: R7 `: r5 B- ?; H, a7 Z1 D; s+ T0 c$ M7 \- N: W
|
|