📌  相关文章
📜  mysqli::real_connect():服务器请求客户端未知的身份验证方法 [caching_sha2_password (1)

📅  最后修改于: 2023-12-03 15:33:04.263000             🧑  作者: Mango

mysqli::real_connect(): server requested client unknown authentication method [caching_sha2_password]

Introduction

If you are a PHP developer and use MySQL as your database, you might have encountered the error message 'mysqli::real_connect(): server requested client unknown authentication method [caching_sha2_password]'. In this article, we will discuss what this error means and how to fix it.

Background

The error usually occurs when the MySQL server version is 8.0 or higher, and you are using PHP version 5.6 or lower. The reason for this error is that MySQL server 8.0 uses a new authentication method called 'caching_sha2_password' which is not supported by older versions of PHP.

Solution

To fix this error, you need to either upgrade your PHP version to 7.0 or higher or use a lower version of MySQL server that supports the older authentication method. However, upgrading PHP is the recommended solution since older versions are no longer supported and may have security vulnerabilities.

Once you have upgraded your PHP version, you can modify your code to use the new authentication method. You can do this by modifying your connection string to include the option 'MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT'. Here's an example of a connection string that includes this option:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_database', null, null, MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);

Alternatively, you can modify the MySQL server configuration to use the older authentication method. To do this, you need to add the following line to the 'mysqld' section of your 'my.cnf' file:

default_authentication_plugin=mysql_native_password
Conclusion

The 'mysqli::real_connect(): server requested client unknown authentication method [caching_sha2_password]' error is a common issue faced by PHP developers using MySQL 8.0 or higher. However, upgrading your PHP version to 7.0 or higher can resolve the issue. Once you have upgraded your PHP version, you can modify your code or MySQL server configuration to use the new authentication method.