52AV手機A片王|52AV.ONE

標題: 解決 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').
  V3 P' N. d9 }7 E1 a) ^
; ^2 r5 {# L0 |+ ]& S: t* i2 |5 g# B+ s# D這是因為php 5.3以前舊版的密碼採16位編碼,而新版php 5.3以後採41位編碼,而mysql 5.2 以前預設也是16碼,所以才會造成此種錯誤.
: R; i2 o% q: U先診斷一下mysql: (我的php 5.6.38 , mysql 5.1.73)* \  s4 a" j8 z! I) U% n
登入mysql 然後輸入 :
; k3 p0 c$ w' M8 G, n9 Ymysql> SHOW VARIABLES LIKE 'old_passwords';
' C4 O3 ]/ k. w# w) t/ P  `( z) [+------------------+-------+( L( X7 Z& Y+ z3 I' P
| Variable_name | Value |2 d( r% `8 P2 Y1 B
+------------------+-------+
, F; F. h# ~* v$ a+ l  V  t1 V; ]| old_passwords | ON   |
4 Y6 R+ b' d, J/ M- w" C+------------------+-------+: Q1 ~0 j, }5 d  w' r+ W
1 row in set (0.00 sec)" K/ G' A* O7 ~7 T

+ I8 ?1 ?, t4 U7 i$ Y& A6 c% }2 vold_password  ==> ON  就表示 /etc/my.cnf  裏的 old_passwords=1  設定為16碼,須將它設為 0 然後重啟mysqld ==> service mysqld restart" A* W4 |2 c1 L# g# ^9 Y! c
或 在 mysql prompt下輸入:* g7 g% _; u2 l3 O
mysql> SET old_passwords=FALSE;  2 j. v( S, m' N( N
檢查mysql.user內 每個密碼長度:. g% h/ q. K5 [6 V" A
mysql> SELECT 'User', 'Host', Length('Password') FROM mysql.user;
0 z+ \$ W5 ^6 _( X如果還沒改成41位,Length('Password')這個欄位應該都是16或是0(表示沒設密碼)
/ B0 T: t5 f4 \* H  d& S. O
3 \2 V# V9 w: u! B" P0 b  {, |8 P再重設原來的密碼:
# R6 C& m: Y' s* I/ J! W: v! Jmysql> SET PASSWORD FOR 'root'@'192.168.1.1' = PASSWORD('原來的密碼');    // 小心要核對原來 帳號@IP 更改,不要改錯了
1 }  s% a) L! h: o, Mmysql> flush privileges;
( g% ^7 V. h% h1 l4 f5 G1 M% w; ]
) b, {; ^7 h; a+ ^0 `: H! @4 s再輸入 SELECT 'User', 'Host', Length('Password') FROM mysql.user; 檢查密碼度,就可發現剛剛改的root 的密碼長度已改為41碼
2 v6 G) A/ r$ w: H% l7 ^7 x0 J7 A注意:
( L8 J+ m' x. w, d, n1 @5 t如果帳號太多,可以遇到問題時再重設密碼,因為重設密碼 SET PASSWORD FOR 只針對個別密碼,不會因為 old_passwords=0 而對所有密碼造成影響
===========================================================================
  L1 O  X* \9 d9 O- H" E當mysql升級到 8.0.21時,php連到mysql出現2行errors:
8 m: j" d- p# u8 n( T: T! Emysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers3 U8 r5 v) R5 Q
mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers
# Y% H3 s* G9 _( t' Y! n- R3 S4 W原因:

1 c4 W5 j' j( W在MySQL 8.0.21中,caching_sha2_password是默認的身份驗證插件,而不是以往的mysql_native_password。所以和php不相容。可以降級php,也可以修改MySQL的配置。 吾人決定修改MySQL的配置:9 }% O: `: v* ]/ Z, w
vi /etc/my.cnf  加入下列:
$ R# V+ G5 Z9 ~$ l# ]9 p6 E[mysqld]6 L. F! v- P; s* d

# u' ~" n3 f0 h' g& b1 t: R- C7 x) Ncharacter-set-server=utf8
& B2 C. \" x! `- S6 X2 \! T. [( Hdefault_authentication_plugin=mysql_native_password
: W, V' s& @9 ?! R% e4 \" w' F! h3 c& ?! ]* n" j# {" k
[mysql]: d7 ~+ G) J/ f4 y' ~* H' g  ^
default-character-set=utf85 k; l& f2 l4 ^7 b' b

/ `) c; o2 P: o7 {[client]
8 i% l6 w2 e2 z9 Ndefault-character-set=utf8! H  a* B- ^" d$ R8 V9 C
6 x9 R9 _4 K' Y- o4 h! n
然後重啟mysqld
% e4 f- v/ J- C3 U" y( |$ q$ a- kservice mysqld restart( y: b; J! N8 F/ G$ f
搞定!!( {4 @5 A/ L" n. d8 |+ `  R& W/ p# J( g

. v( Y4 r( Q% }- U7 w% p
  I7 |$ r4 f5 v6 ~' G) l2 {




歡迎光臨 52AV手機A片王|52AV.ONE (https://www.52av.one/) Powered by Discuz! X3.2