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
Release: 2024-12-13 13:05:10
Original
453 people have browsed it

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]
Copy after login

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';
    Copy after login

    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
    Copy after login

    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';
    Copy after login
  • 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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template