PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client

Today I upgraded the MySQL version to MySQL 8.0.21, but my original Laravel program reported a MySQL connection error.

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

After checking the manual, it turns out that MySQL8 has updated the default encryption method: caching_sha2_password, and PHP does not currently support this encryption method.

To fix this problem, we have to change the default encryption method in MySQL8.

First we change MySQL’s default encryption in the configuration file, open /etc/my.cnf, and add the following in [mysqld] section.

[mysqld] 
default_authentication_plugin= mysql_native_password

Restart MySQL after modifying the configuration file, and then alter the password encryption method of the original user.

ALTER USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Now, everything works well.

RSS