在使用mysql8.0的时候遇到的密码链接问题

时间:2023-03-09 08:39:54
在使用mysql8.0的时候遇到的密码链接问题

问题概述

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

发生这种错误,是由于MySQL 8默认使用了新的密码验证插件:caching_sha2_password,而之前的PHP版本中所带的mysqlnd无法支持这种验证。解决这个问题,有两种办法。

一种办法是升级PHP支持MySQL 8的新验证插件。

PHP 7.2.8和PHP 7.1.20已经可以支持caching_sha2_password,直接连接MySQL 8。

截止PHP 7.0.31和PHP 5.6.37还无法支持caching_sha2_password,不知道后续版本是否会做出支持。

可以通过phpinfo()函数了解当前安装的PHP是否支持caching_sha2_password:

本人解决的办法如下

1 /etc/my.cnf配置文件中,有一行:

# default-authentication-plugin=mysql_native_password

请删除注释符号“#”并重新启动mysqld使之生效,此后创建的账户均默认使用mysql_native_password。

2 登陆MySQL

ALTER USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘new_password‘;

3 如果mysql 链接是使用static  将它改为false