'MySQL authentication plugin set to mysql_native_password but server requests caching_sha2_password

I have an old PHP script that is using mysql_connect("localhost:3306", "root", "password"); to connect to a MySQL server running locally. (Yes, I know this is a security concern; it's not staying that way.)

When I run the script (using PHP5) it says

mysql_connect(): The server requested authentication method unknown to the client [caching_sha2_password]

So, in order to fix this, I went into the database and ran

ALTER USER 'root'@'localhost' identified with mysql_native_password by 'password';
FLUSH privileges;

After restarting the MySQL server, I checked the mysql.user table to confirm that the authentication plugin was now set to mysql_native_password. It is.

I ran the PHP script again, and got the same error.

Then, not knowing what else to do, I opened up etc/mysql/conf.d/mysql.cnf and added the line

default-authentication-plugin=mysql_native_password

I restarted the MySQL server. I ran the PHP script again, and got the same error.

At this point, I don't know what to do? The user (and the server) are both configured to use mysql_native_password; why is it requesting caching_sha2_password?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source