php无法连接mysql8.x

mysql php xue8 1年前 268次浏览



问题描述


php使用mysqli_real_connect链接数据出现


Warning: mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in D:\\php\\test.php on line 8

Warning: mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client in D:\\php\\test.php on line 8
error
=============Connect Error: The server requested authentication method unknown to the client

问题出现的环境背景及自己尝试过哪些方法


出现的环境信息mysql 8.0.13; php 7.2.0; window8;


以下两个方案都不行



试过的方案一:


CREATE USER \'aaa\'@\'localhost\'
IDENTIFIED WITH mysql_native_password BY \'password\';
# 还有
ALTER USER \'aaa\'@\'localhost\' IDENTIFIED BY \'password\' PASSWORD EXPIRE NEVER;
ALTER USER \'aaa\'@\'localhost\' IDENTIFIED WITH mysql_native_password BY \'password\';
FLUSH PRIVILEGES;


方案二:
参考https://segmentfault.com/q/10...

相关代码


// 请把代码文本粘贴到下方(请勿用图片代替代码)
php代码


<?php
$con=mysqli_init();
if (!$con)
{
die("mysqli_init failed");
}

if (!mysqli_real_connect($con,"localhost","aaa","password","wp"))
{
echo "error<br/>=============";
die("Connect Error: " . mysqli_connect_error());
}

mysqli_close($con);
?>

你期待的结果是什么?实际看到的错误信息又是什么?


能够正确连接数据库





回答:

mysql8用了新的加密,你应该修改mysql配置,改成旧的加密,然后重启mysql


回答:

首先,default_authentication_plugin=mysql_native_password
然后,ALTER USER \'aaa\'@\'localhost\' IDENTIFIED WITH mysql_native_password BY \'password\'


0条回复
撰写答案