When using PHP to connect to MySQL 8, the error as shown in the title may occur:
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
This error occurs because MySQL 8 defaults to using the new password Authentication plugin: Caching_sha2_password, and the mysqlnd that was in the previous PHP version did not support this validation. There are two ways to solve this problem.
One way is to upgrade PHP to support MySQL 8 's new authentication plugin.
PHP 7.2.8 and PHP 7.1.20 have been able to support Caching_sha2_password, directly connected to MySQL 8.
As of PHP 7.0.31 and PHP 5.6.37, it is not possible to support Caching_sha2_password, and it is not known whether subsequent versions will support it.
You can find out whether the currently installed PHP supports Caching_sha2_password by using the Phpinfo () function:
If you cannot upgrade PHP, you can create (or modify) an account using the Caching_sha2_password plugin in MySQL 8 to use Mysql_native_password so that previous versions of PHP can be connected.
- When you create user, use identified with Xxx_plugin by ' password ', such as:
-
CREATE USER ' native '@'localhost'with by'password!2#4 ';
- Use ALTER user to modify the validation plugin for an existing account:
ALTER USER ' native '@'localhost' with Mysql_native_password
Or
ALTER USER ' native '@'localhost'with by'new_password ';
In the previous way, the password for the account will be cleared; The BY clause will set a new password for the account.
- In the/ETC/MY.CNF configuration file, there is a line:
-
# Default-authentication-plugin=mysql_native_password
Please delete the comment symbol "#" and restart Mysqld for it to take effect, and the account created thereafter will use Mysql_native_password by default.
- If you complete the installation of MySQL server and modify the/ETC/MY.CNF configuration without starting the MYSQLD service, then the ' root ' @ ' localhost ' account created after the mysqld is started is also used Mysql_native_ Password plug-in.
In this respect, hope is helpful.
PHP Error: sqlstate[hy000] [2054] The server requested authentication method unknown to the client