(1)登录mysql8.0,查看用户信息:
[root@184-8 ~]# cd /home/mysql
[root@184-8 mysql]# ./bin/mysql -uroot -pdbelex1306 -S status/mysql.sock
#执行SQL查询用户信息
select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| 10.% | backup | caching_sha2_password | $A$005$ct:hpS <WHs
t82bx2aVvZO6Gs.rRZrzPb4tcCFJVpxsXC76R4wgzV95 |
| 10.% | root | caching_sha2_password | $A$005$]}!Pw3+j.u)!Jb3J.iNux1Ku3tPXgJTPbx6bJ2QURGB29rAtZihjUtT1 |
| localhost | backup | caching_sha2_password | $A$005$=X#-jOD<[&bF#wUS75e4rSu3Dhkf5Wk.vb6yb7Ymy/bJXZU.0tpzWddTrO3 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | $A$005$Hqcm_p^XEUXbgmIrkG9TbRwBoN7OwsAbuMwP9SPlB2REijtrJYZFu.D |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
7 rows in set (0.00 sec)
#发现每个用户的plugin都是 caching_sha2_password,在上网搜索后,发现这种情况是因为mysql8.0 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password,因为加密方式不同,所以导致php连接不上
(2)修改加密方式
#因为是连接的root,所以更改root的密码
ALTER USER 'root'@'10.%' IDENTIFIED WITH mysql_native_password BY 'aaaaaaaaa';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'aaaaaaaaa';
#也可以直接修改加密规则
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
#再次查看用户信息
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| 10.% | backup | caching_sha2_password | $A$005$ct:hpS <WHs
t82bx2aVvZO6Gs.rRZrzPb4tcCFJVpxsXC76R4wgzV95 |
| 10.% | root | mysql_native_password | *796480FEEE478491C7B3C1792721002EC6E69BAA |
| localhost | backup | caching_sha2_password | $A$005$=X#-jOD<[&bF#wUS75e4rSu3Dhkf5Wk.vb6yb7Ymy/bJXZU.0tpzWddTrO3 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | mysql_native_password | *796480FEEE478491C7B3C1792721002EC6E69BAA |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
#可以看到plugin变为了mysql_native_password,重新进行连接,发现连接成功,insert语句执行成功