關於php7.3.3 連接mysql8 出現 The server requested authentication method unknown to the client

主要原因 是因爲 mysql 8 升級了密碼的驗證方式 caching_sha2_password, 之前一直是mysql_native_password方式

不過  我很好奇 爲什麼php都升級到7.3.3了,爲什麼還不支持caching_sha2_password ,無語。

解決辦法:

1.找到mysql的配置文件my.cnf,我的配置文件位置在   /usr/local/etc/my.cnf (請自行找到自己的電腦的配置文件地址)。

2.在 [mysqld]下加入 default-authentication-plugin=mysql_native_password  一行

3.重啓mysql(我的重啓命令爲mysql.server   restart)

4.進入mysql  修改登入mysql用戶的登錄驗證方式

      ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

           5. 重新回到php嘗試連接mysql

      獲得的經驗:如果知道要連接的mysql版本爲8以及8以上 ,最好在創建用戶的時候加上

CREATE USER 'root123'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root123';

來指定驗證方式。