PHP cannot connect to mysql8 (encryption plug-in problem)

php7 still can't connect to mysql8

//连接数据库
$conn=mysqli_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
if (mysqli_connect_errno()) {
    
    
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

Please add image description

Many tutorials say to add configuration in the /etc/my.cnf configuration file:

default-authentication-plugin=mysql_native_password

Then restart mysql and create a user

CREATE USER 'native'@'localhost' IDENTIFIED  BY 'password'

But in fact, after I tried it, it still didn't work. Check the database

SELECT Host,User,plugin FROM mysql.user;

Check the encryption plugin used by the user.
Insert image description here
The previously created user still uses the caching_sha2_password plugin. At this time, when we create a user, we can specify what plug-in to use. This is more stable. After creating it, it is best to see what plug-in is used.

CREATE USER 'native'@'localhost' IDENTIFIED WITH mysql_native_password  BY 'password'

Guess you like

Origin blog.csdn.net/weixin_45654405/article/details/125082625