1. 程式人生 > >PHP 連線mysql8 報requested authentication method unknown to the client [caching_sha2_password]

PHP 連線mysql8 報requested authentication method unknown to the client [caching_sha2_password]

新裝了環境,裝了mysql8,PHP連線mysql時總是報:

PHP Warning:  mysqli::mysqli(): The server requested authentication method unknown to the client [caching_sha2_password] 

雖然只是一個警告但就是連線不上資料庫,摸索了好久才發現從Mysql5 到Mysql8啟用了新的加密方法。

解決辦法:修改mysql配置檔案my.cnf 使資料庫啟用相容的加密方式

sudo vi /usr/local/etc/my.cnf ,在最下面新增如下程式碼:

[mysqld]
default_authentication_plugin= mysql_native_password

儲存退出,重啟mysql。

再次執行PHP成功連線mysql。

mac下啟動,重啟mysql命令:

啟動MySQL服務  mysql.server start
停止MySQL服務  mysql.server stop
重啟MySQL服務  mysql.server restart