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').! U) x( B; j0 g, w6 s! |
- L' r+ u# s9 P$ O, M, w
這是因為php 5.3以前舊版的密碼採16位編碼,而新版php 5.3以後採41位編碼,而mysql 5.2 以前預設也是16碼,所以才會造成此種錯誤.- T) `$ N, f1 ^$ n% S
先診斷一下mysql: (我的php 5.6.38 , mysql 5.1.73)
" q, _( F% A& R" b, O登入mysql 然後輸入 : 2 v# O  G, z0 b: W+ [4 n7 i8 g; j
mysql> SHOW VARIABLES LIKE 'old_passwords';
2 `3 `5 g& J) R  f) z- |+------------------+-------+
0 R( s2 b6 z0 g1 T9 ?. o| Variable_name | Value |+ `) a  J( v  T) j
+------------------+-------+
0 n$ n+ z4 t8 Z0 t$ Q. e| old_passwords | ON   |5 D4 l- {, T% g
+------------------+-------+
; V2 V  ^- q' \1 row in set (0.00 sec)
; ^) F: X0 x" q  s, ~% p7 }6 B
- l7 r0 h) a' u( Hold_password  ==> ON  就表示 /etc/my.cnf  裏的 old_passwords=1  設定為16碼,須將它設為 0 然後重啟mysqld ==> service mysqld restart, l) r5 Z: C7 _7 ]8 [+ {: a
或 在 mysql prompt下輸入:: s8 a0 R) A  A& Y/ }
mysql> SET old_passwords=FALSE;  
  ^3 e, Q9 U: X+ N5 C7 j7 R4 b檢查mysql.user內 每個密碼長度:% Y$ }4 m6 U4 d* o0 u5 \8 K
mysql> SELECT 'User', 'Host', Length('Password') FROM mysql.user;
# P, y  Y, y! t- b9 H/ |" q/ i% c如果還沒改成41位,Length('Password')這個欄位應該都是16或是0(表示沒設密碼) - Y  N0 W% }- @) T+ p8 Z" S, W

! C) p' A% A$ u( K再重設原來的密碼:
& x- C5 Q$ B/ k. ~" Wmysql> SET PASSWORD FOR 'root'@'192.168.1.1' = PASSWORD('原來的密碼');    // 小心要核對原來 帳號@IP 更改,不要改錯了
6 E: e0 ~4 i- O0 J' B) K) m3 Dmysql> flush privileges;4 o( ~8 D8 H7 U
4 i  H4 W+ h% {- \
再輸入 SELECT 'User', 'Host', Length('Password') FROM mysql.user; 檢查密碼度,就可發現剛剛改的root 的密碼長度已改為41碼
2 V& D9 ^* c  ~/ V3 Q  h5 \! {注意:! _' y- F" \& G5 b
如果帳號太多,可以遇到問題時再重設密碼,因為重設密碼 SET PASSWORD FOR 只針對個別密碼,不會因為 old_passwords=0 而對所有密碼造成影響
===========================================================================% V2 o, ?9 l+ f( k5 p* y: a0 V
當mysql升級到 8.0.21時,php連到mysql出現2行errors:
* K8 @6 U/ j% f* [9 m- Z: kmysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers" U& U& S- T6 @/ Y2 y$ {7 \" Y2 h7 {
mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers
, [9 {! C; M$ k! {& Q! W' S原因:

6 u- ]0 o7 ^1 ]( _5 X* m* s在MySQL 8.0.21中,caching_sha2_password是默認的身份驗證插件,而不是以往的mysql_native_password。所以和php不相容。可以降級php,也可以修改MySQL的配置。 吾人決定修改MySQL的配置:7 C) ^, j7 S; t
vi /etc/my.cnf  加入下列:/ K! I2 C0 @) g' |, R: U
[mysqld]/ p( J5 h, b! E* }3 s, {  Z: w
+ ]) F' Z% Q% K0 q  ^
character-set-server=utf88 S4 c% G3 H% {! N# m6 [. L7 P
default_authentication_plugin=mysql_native_password! T# s+ j0 }% ]
" g5 J9 D0 [' L
[mysql]
+ h& w8 I& }) p7 p, X3 rdefault-character-set=utf8
( _; U2 m4 L$ Z. @* K, ?- F5 x# y3 O' B' c: S4 c
[client]9 U2 y% U9 y. b- s% {! f
default-character-set=utf8
  a" r5 ?- F; C4 y2 F7 I/ G. I! E) k0 y8 {6 K) u% S
然後重啟mysqld9 a* G2 p8 a( S5 `+ f
service mysqld restart: i- V+ _" y- ]3 h
搞定!!
7 m8 s, o: j! I; X

; W: f5 r$ _$ e3 ]6 @8 c* L: M. B* P  H7 q$ k0 w9 r





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