Home>Article>Backend Development> What should I do if php connects to mysql8 and reports an error?
Solution to PHP error when connecting to mysql8: 1. Turn off the comment in front of "mysql_native_password" in "/etc/my.cnf"; 2. Enter mysql and update the password with "mysql_native_password" That’s it.

The operating environment of this tutorial: CentOS 7 system, mysql8 version, DELL G3 computer
#What should I do if php connects to mysql8 and reports an error?
Solution to PHP connection failure to MYSQL8
phpexploded and didn’t come out for two hours.
mysqli_error()and there was no output. I checked the PHP related components and themysqllog but there was no response. Since it is a cloud host, I tried Changedlocalhostto127.0.0.1, still to no avail.
mysqlversion,mysql8, which is too difficult for people who are still usingmysql5, so I checked it out After getting an update onmysql8, I picked up the child, haha.

mysql8, a new verification methodcaching_sha2_passwordappeared, and this method became the new The verification mechanism is also processed in this way by default when configuring the password. The originalmysql_native_passwordis replaced. This is whyphpcannot be connected.
https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching -sha2-password
/etc/my.cnf(centos for students) Turn off the comment in front ofmysql_native_password.. That is, change back to the old verification method:
[mysqld] default_authentication_plugin=mysql_native_password2. Enter
mysqland update the password usingmysql_native_password.
mysql>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; mysql>flush privileges;Or you can add a new user.
mysql>CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; mysql>GRANT ALL PRIVILEGES ON *.* TO 'test'@'%'; mysql>flush privileges;The problem is successfully solved.
phpand I can’t use the new verification method. Then this is not useless, so It can certainly be solved usingphp, so I looked at it again and found something.
mysql_xdevapithat can be supported, but beforephp7.2.4it was eitherphp PDOorphp mysqlicannot support the new verification method.
PHP Video Tutorial"
The above is the detailed content of What should I do if php connects to mysql8 and reports an error?. For more information, please follow other related articles on the PHP Chinese website!