PHP mysqli_connect : authentication method unknown to the client [caching_sha2_password]

标签 php mysql hash sha

我正在使用 php mysqli_connect 登录 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 表: mysql.user table

MySQL 服务器 ini 文件:

[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

在MySQL Server ini文件中使用caching_sha2_password,根本不可能使用user1或user2登录;

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

在MySQL Server ini文件中使用mysql_native_password,可以用user1登录,但用user2登录,同样的错误;

<小时/>

如何在 mySql 服务器上使用 caching_sha2_password 登录?

最佳答案

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

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

https://dev.mysql.com/doc/refman/8.0/en/alter-user.html 引用

如果您正在创建新用户

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

https://dev.mysql.com/doc/refman/8.0/en/create-user.html 引用

这对我有用

关于PHP mysqli_connect : authentication method unknown to the client [caching_sha2_password],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58906232/

相关文章:

git - 如何使用 GitLab API 查找文件的提交哈希值?

java - Java中的多层哈希

php - 使用 php mysql 动态创建带有选项组的选项列表

php - 如何从 php 文件中获取已声明函数及其数据的列表?

php - 在 php 页面中显示 MySQL 数据库表中的条目

mysql - 无法在工作台中创建 mysql 表

perl - 将散列传递给子例程

PHP mysql PDO 拒绝设置 NULL 值

php - scraper php 返回空白页

mysql - 使用 if 条件更新 MySQL