One - One Code All

Blog Content

php/python/mysql: authentication method unknown to the client [caching_sha2_password] [duplicate]

PHP Python MySQL   2019-08-18 23:55:39


php/python: authentication method unknown to the client [caching_sha2_password] [duplicate]


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。

可以通过phpinfo()函数了解当前安装的PHP是否支持caching_sha2_password。搜索loaded plugins.


二、在MySQL 8中创建(或修改)使用caching_sha2_password插件的账户,使之使用mysql_native_password,这样先前版本的PHP就可以连接使用了。


在CREATE USER时,使用IDENTIFIED WITH xxx_plugin BY 'password',比如:

CREATE USER 'native'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password!2#4';


使用ALTER USER修改已有账户的验证插件:

ALTER USER 'native'@'localhost' IDENTIFIED WITH mysql_native_password


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

采用前一种方式,账户的密码将被清除;BY子句将为账户设置新的密码。


/etc/my.cnf配置文件:

# default-authentication-plugin=mysql_native_password

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



FLUSH PRIVILEGES; 


/etc/my.cnf:

[mysqld]

default-authentication-plugin=mysql_native_password



上一篇:部署docker apache php5.6
下一篇:centos8安装php7.4

The minute you think of giving up, think of the reason why you held on so long.