Home > Database > Mysql Tutorial > How to Fix 'mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]' Error?

How to Fix 'mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]' Error?

Mary-Kate Olsen
Release: 2024-12-26 15:11:10
Original
744 people have browsed it

How to Fix

php mysqli_connect: Authentication Method Caching_sha2_Password Issue on MySQL Server

Problem:

When attempting to connect to a MySQL database using mysqli_connect on localhost, the following error occurs: "mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]". This issue arises when using the caching_sha2_password authentication plugin on the MySQL server.

Solution:

To resolve this authentication method conflict, follow these steps:

  1. In the MySQL Server ini file (typically named "my.ini" or "my.cnf"), locate the "default_authentication_plugin" setting.
  2. Change the default_authentication_plugin value from "caching_sha2_password" to "mysql_native_password".
  3. Save the ini file and restart the MySQL server.
  4. For existing users, run the following SQL command to reset their authentication method to mysql_native_password:
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Copy after login
  1. For new users, create them using the mysql_native_password authentication method:
CREATE USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Copy after login

By implementing these changes, your MySQL server will be configured to use the mysql_native_password authentication plugin, making it compatible with the mysqli_connect function and allowing for successful database connections.

The above is the detailed content of How to Fix 'mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]' Error?. 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