The server requested authentication method unknown to the client

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 -uroot -p
  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.  use mysql;
  2. UPDATE `user` SET `Host`='%' WHERE `User`='root';
  3. 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!

猜你喜欢

转载自www.cnblogs.com/jdcai/p/9202584.html
今日推荐