標題: 解決 php連接mysql指令mysqli_connect()等等的錯誤==>mysqlnd cannot connect to MySQL 4.1 [打印本頁] 作者: IT_man 時間: 2018-10-11 12:57 標題: 解決 php連接mysql指令mysqli_connect()等等的錯誤==>mysqlnd cannot connect to MySQL 4.1 當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').. |+ f; p" f' I& i
/ a" l* s1 H5 }3 C! V+ g6 P* O
這是因為php 5.3以前舊版的密碼採16位編碼,而新版php 5.3以後採41位編碼,而mysql 5.2 以前預設也是16碼,所以才會造成此種錯誤. . _; \" ^( t. w: K& D先診斷一下mysql: (我的php 5.6.38 , mysql 5.1.73) " O6 H! T7 Y) Y" J0 X登入mysql 然後輸入 : 7 W: x6 t1 q( [. W0 T8 k
mysql> SHOW VARIABLES LIKE 'old_passwords'; 3 n8 W8 C& @6 C+ E3 Q; e+------------------+-------+5 S0 y. h2 M1 N Z: v+ W# y# U e
| Variable_name | Value | 9 x) o# j7 r9 f! H: x/ T- B$ x) D+------------------+-------+ F! `( b5 m' u z' g0 h
| old_passwords | ON |. C/ r3 |+ r8 {$ q
+------------------+-------+ 5 u+ M0 a {! {" g% w8 }1 row in set (0.00 sec)( G. @8 M, p0 R/ i; Y" I
# l' S/ G' e0 H8 @old_password ==> ON 就表示 /etc/my.cnf 裏的 old_passwords=1 設定為16碼,須將它設為 0 然後重啟mysqld ==> service mysqld restart . ^5 F F, o7 z, _" h* |或 在 mysql prompt下輸入: ( b. m0 X/ d, _0 omysql> SET old_passwords=FALSE; . I: `0 k/ i/ V( Y- v! i1 y檢查mysql.user內 每個密碼長度:* f# K6 E" ?3 s* T, T
mysql> SELECT 'User', 'Host', Length('Password') FROM mysql.user;6 h! g- \9 `4 U5 h7 c
如果還沒改成41位,Length('Password')這個欄位應該都是16或是0(表示沒設密碼) $ a) V' R* z1 ^! K' B Q) o
+ b$ {6 l; I1 [ W再重設原來的密碼:: b1 [% J$ W# j: Y
mysql> SET PASSWORD FOR 'root'@'192.168.1.1' = PASSWORD('原來的密碼'); // 小心要核對原來 帳號@IP 更改,不要改錯了' @$ Y/ c) s, |+ e* K5 D
mysql> flush privileges;0 h6 D. e; j' }
' e* n1 y, }% q0 p- X9 f再輸入 SELECT 'User', 'Host', Length('Password') FROM mysql.user; 檢查密碼度,就可發現剛剛改的root 的密碼長度已改為41碼 - ^/ o; u& ?; i! j8 _6 F4 j6 |注意: ) k# y: {5 S9 N/ j/ j如果帳號太多,可以遇到問題時再重設密碼,因為重設密碼 SET PASSWORD FOR 只針對個別密碼,不會因為 old_passwords=0 而對所有密碼造成影響===========================================================================- f6 q. [1 I5 c1 Q" T/ ]* U
當mysql升級到 8.0.21時,php連到mysql出現2行errors:: Q* B0 t5 D) J# y
mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers , V3 G+ a: Y u2 m% b& smysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers4 O, ~7 s% S& H3 J# \" S
原因:8 N" c7 ^4 d U' W. t* ~9 d
在MySQL 8.0.21中,caching_sha2_password是默認的身份驗證插件,而不是以往的mysql_native_password。所以和php不相容。可以降級php,也可以修改MySQL的配置。 吾人決定修改MySQL的配置:' _) p* o$ h8 y3 t3 s2 r1 P
vi /etc/my.cnf 加入下列: $ E, K; r0 I5 b) D/ P0 G[mysqld]3 X" v, N" j1 a$ e) V' v# b! ]
* L) I( {( v C& L
character-set-server=utf8 % ~3 R. P0 W0 Tdefault_authentication_plugin=mysql_native_password 5 D. C& Y0 H; d( c3 k2 M2 D& v( t6 M; b+ R
[mysql] ) B; u6 ~; U3 j& ~& B' D2 a1 hdefault-character-set=utf8 V. c' S1 q: h, q& M
3 n0 V4 N' x. i[client]6 r5 O7 k3 i" U2 f }3 k# ?
default-character-set=utf8 * L( _. g3 R1 `& E0 C ) u" y: n. @# N然後重啟mysqld- C9 M- n1 M3 z( k' L/ z
service mysqld restart7 [% f( X/ `* j6 l+ g
搞定!! ! \5 k* i3 u \2 l" r + F* z3 b5 j8 D" Z0 e1 ]1 r- @4 }) f0 O! a5 }3 `3 A9 b8 C1 Q