Home >Backend Development >PHP Tutorial >Why Does mysqli_connect Fail with 'Authentication Method Unknown (caching_sha2_password)'?

Why Does mysqli_connect Fail with 'Authentication Method Unknown (caching_sha2_password)'?

Linda Hamilton
Linda HamiltonOriginal
2024-12-13 13:05:10433browse

Why Does mysqli_connect Fail with

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:

  1. 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.

  2. 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';
  • Grant the necessary privileges to the user after altering the authentication plugin or modifying the Server ini file.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn