php7.4连接MySQL8.0报错 PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]")

原因:
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)

  

posted @ 2020-12-15 11:17  Ambtin  阅读(579)  评论(0编辑  收藏  举报