记录一次php连接mysql8.0失败

Posted 礁之

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录一次php连接mysql8.0失败相关的知识,希望对你有一定的参考价值。

连接代码:(php5.3.29if (!empty($user_login_time)) 
                            $ls_mail_ip = "10.173.217.8";
                            $ls_mail_database = "ls_mail";
                            $ls_mail_sql = "insert into mail values('$touser_id',$user_login_time,'$reward_id',$tag)";
                            print_r($ls_mail_sql."\\n");
                            $insert_ls_mail = new mysqli($ls_mail_ip, "root", "aaaaaaaaa", $ls_mail_database);
                            if(mysqli_connect_error())
                                echo mysqli_connect_error();
                            
                            $insert_result = $insert_ls_mail->query($ls_mail_sql);
                            if($result === false)
                                echo $insert_ls_mail->error;
                                echo $insert_ls_mail->errno;
                            
                            echo $insert_ls_mail->num_rows;
                            echo $insert_ls_mail->insert_id;
                            $insert_ls_mail->close();
                          
连接失败,报错:
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Warning: mysqli::query(): Couldn't fetch mysqli in /renzeyuan/test.php on line 104
。。。。。。
(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语句执行成功

以上是关于记录一次php连接mysql8.0失败的主要内容,如果未能解决你的问题,请参考以下文章

PHP无法连接MYSQL8.0一般处理办法

MySQL8.0以上版本创建用户并授权远程连接

CentOs7中Mysql8.0设置远程连接

navicate 12 连接 mysql8.0 使用日志

微信小程序华莱士获取用户信息失败

使用Navicat远程连接MySQL8.0+版本