如何解决php mysqli_connect: authentication method unknown to the client [caching_sha2_password]的问题?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (1922)

使用php mysqli_连接到MySQL数据库(全部在本地主机上)并进行登录:

<?php
//DEFINE ('DB_USER', 'user2');
//DEFINE ('DB_PASSWORD', 'pass2');
DEFINE ('DB_USER', 'user1');
DEFINE ('DB_PASSWORD', 'pass1');
DEFINE ('DB_HOST', '127.0.0.1');
DEFINE ('DB_NAME', 'dbname');

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if(!$dbc){
    die('error connecting to database');    
}
?>

这是mysql.user表:

MySQLServerini文件:

[mysqld]
# The default authentication plugin to be used when connecting to the server
default_authentication_plugin=caching_sha2_password
#default_authentication_plugin=mysql_native_password

但出现以下报错:

> error: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in...

with "mysql_native_password" in the mySql Server ini file, it's possible to login with user1, but with user2, same error;

该如何解决呢?

提问于
用户回答回答于

php mysqli扩展不支持新的caching_sha2身份验证功能,得等到他们发布新版本

用户回答回答于

通过SQL命令解决这个问题:

ALTER USER 'mysqlUsername'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mysqlUsernamePassword';

如果正在创建新用户,输入以下代码:

 CREATE USER 'jeffrey'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

扫码关注云+社区

领取腾讯云代金券