search

Home  >  Q&A  >  body text

php mysqli_connect: Authentication method unknown to client

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粉170858678P粉170858678417 days ago702

reply all(2)I'll reply

  • P粉727416639

    P粉7274166392023-10-14 16:49:08

    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

    reply
    0
  • P粉225961749

    P粉2259617492023-10-14 15:47:19

    As of PHP 7.4, this is no longer a problem. mysqlnd added support for the caching_sha2 authentication method.


    Currently, the new caching_sha2 authentication feature is not supported by the PHP mysqli extension. You'll have to wait until they release an update.

    View related posts from MySQL developers: https: //mysqlserverteam.com/upgrading-to-mysql-8-0-default-authentication-plugin-considerations/

    They don't mention PDO, maybe you should try using PDO connection.

    reply
    0
  • Cancelreply