Home >Backend Development >PHP Tutorial >Why Does mysqli_connect Fail with 'Authentication Method Unknown (caching_sha2_password)'?
mysqli_connect Error: Authentication Method Unknown (caching_sha2_password)
When attempting to authenticate with MySQL using mysqli_connect, you may encounter the error:
mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
This error occurs when the MySQL Server's default authentication plugin is set to caching_sha2_password, which is incompatible with certain user account configurations.
Troubleshooting:
To resolve this issue, two solutions are available:
Alter User Authentication Plugin:
Run the following SQL command to change the authentication plugin for the affected user:
ALTER USER 'username'@'hostname' IDENTIFIED WITH mysql_native_password BY 'password';
Replace username and hostname with the appropriate values for your user.
Modify MySQL Server Ini File:
Edit the MySQL Server's ini file (my.ini or my.cnf) and change the default_authentication_plugin setting to mysql_native_password:
[mysqld] default_authentication_plugin=mysql_native_password
Restart the MySQL Server after making the change.
Additional Tips:
If you are creating a new user, use the following command with mysql_native_password authentication:
CREATE USER 'username'@'hostname' IDENTIFIED WITH mysql_native_password BY 'password';
By following these steps, you can successfully authenticate with MySQL using mysqli_connect even when the caching_sha2_password authentication plugin is enabled.
The above is the detailed content of Why Does mysqli_connect Fail with 'Authentication Method Unknown (caching_sha2_password)'?. For more information, please follow other related articles on the PHP Chinese website!