找回密码
 新建账号

The server requested authentication method unknown to the client

[复制链接]
php 发表于 2018/4/26 22:23 | 显示全部楼层 |阅读模式
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client.

In MySQL 8.0+, the default authentication plugin has changed from 'mysql_native_password' to 'caching_sha2_password', and the 'root'@'localhost' administrative account uses the 'caching_sha2_password' authentication plugin by default. If you prefer that the root account use the previous default authentication plugin 'mysql_native_password'.

The PDO_MySQL and ext/mysqli extensions of PHP do not support caching_sha2_password. In addition, when PHP MySQL extensions used with PHP versions before 7.1.16 and PHP 7.2 before 7.2.4, they fail to connect with default_authentication_plugin=caching_sha2_password even if caching_sha2_password is not used. However the X DevAPI PHP extension (mysql_xdevapi) supports it.

To restore MySQL pre-8.0 compatibility, you can reconfigure the server to revert to the previous default authentication plugin mysql_native_password like:
  1. [mysqld]
  2. default_authentication_plugin=mysql_native_password
复制代码
Restart the MySQL database, the problem should be solved, otherwise you may need to reinitialize the MySQL installation data and reinstall it.

You can connect to the MySQL database using mysql commandline utility, and then create a new user with the older mysql_native_password plugin with SQL.
  1. mysql -hwuxiancheng.cn -uroot -p -P3306 mysql
复制代码
  1. CREATE USER 'WUXIANCHENG' IDENTIFIED WITH 'mysql_native_password' BY 'YOUR_PASSWORD_IN_TRANSPARENT_TEXT';
  2. GRANT ALL PRIVILEGES ON *.* TO 'WUXIANCHENG'@'%';
复制代码
Now you can connect to the MySQL database in PHP using the newly created account. You can also change existing users' authentication plugin after connecting to the MySQL database with the new account.
  1. UPDATE `user` SET `Host`='%' WHERE `User`='root';
  2. ALTER USER 'root' IDENTIFIED WITH 'mysql_native_password' BY 'YOUR_PASSWORD_IN_TRANSPARENT_TEXT';
复制代码
If the default root user was restricted to localhost, the ALTER USER statment will fail with message like "ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%'", so you have to change Host from localhost to % first.
Dont' forget to replace YOUR_PASSWORD_IN_TRANSPARENT_TEXT with your own password!

PHP 7.3.0+ supports the caching_sha2_password authentication plugin.

手机版|轻松E站

GMT+8, 2024/3/19 18:55

快速回复 返回顶部 返回列表