Home >Database >Mysql Tutorial >Why Does mysqli_connect() Fail with 'Authentication Method Unknown (Caching_sha2_password)'?
mysqli_connect: Authentication Method Unknown (Caching_sha2_password)
Background:
When using MySQL's mysqli_connect() function, an error can arise: "error: mysqli_connect(): The server requested authentication method unknown to the client." This indicates a mismatch between the authentication method used by the server and that implemented by the client.
Specifically, if the MySQL server is set to use the caching_sha2_password authentication method, the error can occur when the client does not support or is not configured to use that method.
Troubleshooting and Solution:
In the given case, the MySQL Server is configured to use caching_sha2_password authentication, which the PHP client is not equipped to handle. To resolve this issue, you can:
1. Modify MySQL Server Configuration:
Change the default_authentication_plugin setting in MySQL's ini file (my.ini on Windows, /etc/my.cnf on Linux) from caching_sha2_password to mysql_native_password. This will allow the server to use the older authentication method.
2. Update PHP Client:
Upgrade the PHP MySQL client to a version that supports caching_sha2_password authentication. However, this may not be necessary, as the server configuration change should rectify the issue.
3. Set User Authentication:
Run the following SQL command in MySQL:
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
This command will alter the authentication method for the specified user to mysql_native_password, allowing them to connect despite the server's caching_sha2_password setting.
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!