PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unkno…

今天安装安装一个叫得推校园O2O系统的使劲都说连接不上服务器。

下面是安装说明,我直接进行3步骤,导入数据库配置文件,结果就显示题目报错的内容

安装说明: 直接输入程序目录即可 http://localhost/ 程序只支持站点根目录
安装问题说明:
1.跳转不到安装目录
删除config/install.lock
2.安装出现temp/caches不可写提示
查看目录是否存在,不存在则创建
设置目录拥有读写权限
3.不能正常安装,一直停留在安装界面
可以直接导入install/xyo2o.sql,创建config/install.lock,更改config/config.php数据库配置
后台默认密码 admind admin 如果不是清空admin表
4.访问出现请关闭全局变量
请在php.ini 中设置register_globals=off
5.打不开网站
请删除.htaccess文件,或者配置伪静态 

然后就是排错啊。找啊找原来我装的mysql是8.0的

错误信息

SQLSTATE[HY000] [2054] The server requested authentication method unknown to...

这个错可能是mysql默认使用 caching_sha2_password
作为默认的身份验证插件,而不再是 mysql_native_password
,但是客户端暂时不支持这个插件导致的。 官方文档说明

In MySQL 8.0, caching_sha2_password is the default authentication plugin rather than mysql_native_password. For information about the implications of this change for server operation and compatibility of the server with clients and connectors, see caching_sha2_password as the Preferred Authentication Plugin.

在MySQL 8.0中,caching_sha2_password是默认的身份验证插件,而不是mysql_native_password。有关此更改对服务器操作的影响以及服务器与客户端和连接器的兼容性的信息,请参阅caching_sha2_password作为首选身份验证插件。

解决方法一:修改MySQL全局配置文件

编辑 my.cnf
文件,更改默认的身份认证插件。

vi /etc/my.cnf

在 [mysqld]
中添加下边的代码

default_authentication_plugin=mysql_native_password

然后重启mysql

 service mysqld restart

解决方法二:修改密码认证方式

ALTER USER 'YOURUSERNAME'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOURPASSWORD';

第一个方法我试了不行,我使用修改密码认证方式成功的。修改后再到网站首页创建数据库什么的就不会报错服务器连接失败了。还好我是直接导入数据库修改配置文件后她才能出现这个具体的报错。

参考链接:https://www.colabug.com/3497990.html

参考链接:https://www.lanrenzhijia.com/source/5299.html

原文地址:https://www.cnblogs.com/Crazy-Liu/p/10944467.html