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').
) Y' P1 r) P  {6 k7 |2 o, C+ J4 f9 O: j  d5 E# K
這是因為php 5.3以前舊版的密碼採16位編碼,而新版php 5.3以後採41位編碼,而mysql 5.2 以前預設也是16碼,所以才會造成此種錯誤.! U! e) y+ {% J' H( w
先診斷一下mysql: (我的php 5.6.38 , mysql 5.1.73)6 g- q$ Z' }  A. M' a9 x+ X. V1 L( `
登入mysql 然後輸入 : % E) o7 ~4 O5 q, R- o
mysql> SHOW VARIABLES LIKE 'old_passwords';
( ?4 Y& M5 ~" K7 h- ~7 U, _3 T+------------------+-------+; }: Y: o  `/ T# V4 l9 p6 {' J+ j
| Variable_name | Value |
  K- d/ I8 Y: g4 ~! k+------------------+-------+5 a0 V; }- ~4 y3 ~# B- J
| old_passwords | ON   |
% B9 [# n8 K# ^- b3 p+------------------+-------+
( Z3 I; \1 x$ b# X  v1 row in set (0.00 sec)* g" d+ G( s0 \; t% w1 P; }
0 M5 j, ]% `+ P, ]
old_password  ==> ON  就表示 /etc/my.cnf  裏的 old_passwords=1  設定為16碼,須將它設為 0 然後重啟mysqld ==> service mysqld restart
6 d0 P5 _( S' k5 ^+ d或 在 mysql prompt下輸入:
# _1 f7 `" M7 @# Jmysql> SET old_passwords=FALSE;  
0 \+ J4 V! j, H% G  Z8 A7 \) E6 ]( B, q檢查mysql.user內 每個密碼長度:) \. J/ g$ r1 A% i8 G' V5 d
mysql> SELECT 'User', 'Host', Length('Password') FROM mysql.user;
, }8 g+ A0 {3 [) z7 T8 W如果還沒改成41位,Length('Password')這個欄位應該都是16或是0(表示沒設密碼) 6 H: Z) B. l/ C3 O# R

+ m; F3 d: |- _" }再重設原來的密碼:
/ C( X# p8 o1 e7 b" Rmysql> SET PASSWORD FOR 'root'@'192.168.1.1' = PASSWORD('原來的密碼');    // 小心要核對原來 帳號@IP 更改,不要改錯了
0 L( v% b! N+ [0 wmysql> flush privileges;7 d- ^1 w1 Q6 N. t2 h
0 ^8 W. E$ u& j- u4 H! H
再輸入 SELECT 'User', 'Host', Length('Password') FROM mysql.user; 檢查密碼度,就可發現剛剛改的root 的密碼長度已改為41碼7 o1 Y" t7 h: I' E2 }) Q  }
注意:, L$ R% T- ^  u; M9 r9 k
如果帳號太多,可以遇到問題時再重設密碼,因為重設密碼 SET PASSWORD FOR 只針對個別密碼,不會因為 old_passwords=0 而對所有密碼造成影響
===========================================================================
' q; ?' |& i) F1 b/ d當mysql升級到 8.0.21時,php連到mysql出現2行errors:$ n9 D: {! m4 z! t
mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers) `6 B( I+ W9 p3 `' P: i! f* a
mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers- w6 \1 M9 W8 x
原因:
' p, H/ c7 ?$ o
在MySQL 8.0.21中,caching_sha2_password是默認的身份驗證插件,而不是以往的mysql_native_password。所以和php不相容。可以降級php,也可以修改MySQL的配置。 吾人決定修改MySQL的配置:# `6 T! b1 h/ S( ]
vi /etc/my.cnf  加入下列:  ]5 c. ]2 w4 a" w5 D1 C
[mysqld]
2 F' M, I* S/ p5 g7 M4 W! W) v4 I+ G; q; U( |9 `
character-set-server=utf8
  n& E. N% O% P  o9 X7 |2 idefault_authentication_plugin=mysql_native_password
4 s! x4 b6 {8 [! K  X1 N; \5 ?& T6 M! S& I1 W/ T
[mysql]# p3 p7 |) u/ U8 e
default-character-set=utf8* P$ a( j; h) J' t4 }3 _9 s
4 D/ Y( x2 s* W  T' R
[client]
1 Y. e- D1 d( t$ Y! Gdefault-character-set=utf83 E4 w* [+ ?6 o8 e# @) t  _- T

/ U2 H$ z9 L  f; l* }6 A+ z  l然後重啟mysqld5 R1 I5 V: q7 s, n' B
service mysqld restart  o) X) D$ ]& t9 p* N
搞定!!
$ d" g% J$ N& U6 t& [
! F- `6 r9 \0 P. a/ C7 G1 C+ p
7 I4 D8 n" V* [# b





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