php7.4連線MySQL8.0報錯 PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]")
阿新 • • 發佈:2020-12-15
原因:
mysql8預設的使用密碼認證方式不一樣,mysql8.0預設使用caching_sha2_password,但是之前版本都是使用mysql_native_password
解決方案:
將密碼認證方式caching_sha2_password修改為mysql_native_password
mysql> select user,host,plugin from mysql.user; +------------------+-----------+-----------------------+ | user | host | plugin | +------------------+-----------+-----------------------+ | mysql.infoschema | localhost | caching_sha2_password | | mysql.session | localhost | caching_sha2_password | | mysql.sys | localhost | caching_sha2_password | | root | localhost | caching_sha2_password | +------------------+-----------+-----------------------+ 4 rows in set (0.00 sec)
修改root身份的"localhost"
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
如果有root身份的"%"也改一下
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
再次檢視
mysql> select user,host,plugin from mysql.user; +------------------+-----------+-----------------------+ | user | host | plugin | +------------------+-----------+-----------------------+ | mysql.infoschema | localhost | caching_sha2_password | | mysql.session | localhost | caching_sha2_password | | mysql.sys | localhost | caching_sha2_password | | root | localhost | mysql_native_password | +------------------+-----------+-----------------------+ 4 rows in set (0.09 sec)