php mysqli_connect: Authentication method unknown to client
P粉170858678
P粉170858678 2023-10-13 18:02:37
0
2
404

I'm using php mysqli_connect Login to MySQL database (all on localhost)

<?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');    
}
?>

This is the mysql.user table:

MySQL server ini file:

[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

Using caching_sha2_password in the MySQL Server ini file, it is impossible to log in with user1 or user2;

Error: mysqli_connect(): Server requested client [caching_sha2_password] unknown authentication method...

Using mysql_native_password in the MySQL Server ini file, you can log in with user1, but the same error occurs with user2;


How to log in using caching_sha2_password on mySql server?

P粉170858678
P粉170858678

reply all(2)
P粉727416639

I solved this problem via SQL command:

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

Referenced by https://dev.mysql. com/doc/refman/8.0/en/alter-user.html

If you are creating a new user

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

Referenced by https://dev.mysql. com/doc/refman/8.0/en/create-user.html

This worked for me

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!