PHP错误:SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

使用PHP连接MySQL 8的时候,可能会发生如标题所示的错误:

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

发生这种错误,是由于MySQL 8默认使用了新的密码验证插件:caching_sha2_password,而之前的PHP版本中所带的mysqlnd无法支持这种验证。解决这个问题,有两种办法。

一种办法是升级PHP支持MySQL 8的新验证插件。

PHP 7.2.8和PHP 7.1.20已经可以支持caching_sha2_password,直接连接MySQL 8。

截止PHP 7.0.31和PHP 5.6.37还无法支持caching_sha2_password,不知道后续版本是否会做出支持。

可以通过phpinfo()函数了解当前安装的PHP是否支持caching_sha2_password:

如果不能升级PHP,可以在MySQL 8中创建(或修改)使用caching_sha2_password插件的账户,使之使用mysql_native_password,这样先前版本的PHP就可以连接使用了。

  1. 在CREATE USER时,使用IDENTIFIED WITH xxx_plugin BY ‘password‘,比如:

    CREATE USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘password!2#4‘;
  2. 使用ALTER USER修改已有账户的验证插件:

    ALTER USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password

    ALTER USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘new_password‘;

    采用前一种方式,账户的密码将被清除;BY子句将为账户设置新的密码。

  3. /etc/my.cnf配置文件中,有一行:

    # default-authentication-plugin=mysql_native_password

    请删除注释符号“#”并重新启动mysqld使之生效,此后创建的账户均默认使用mysql_native_password。

  4. 如果您完成MySQL Server的安装之后,在没有启动过mysqld服务的情况下修改/etc/my.cnf配置,那么启动mysqld之后创建的‘root‘@‘localhost‘账户也是使用mysql_native_password插件的。

就这些,希望有帮助。

原文地址:https://www.cnblogs.com/cndavidwang/p/9357684.html

时间: 2024-08-01 02:49:34

PHP错误:SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client的相关文章

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

安装 nextcloud 的时候选择的是 mysql ,在链接 mysql 的时候报错,信息如下: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client 搜索了下应该是 MySQL 8默认使用了新的密码验证插件:caching_sha2_password ,而有些 PHP 版本不支持这个问题造成的 解决办法: 修改登录用户的 plugin 为 mysql_native_pas

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

这个错可能是mysql默认使用 caching_sha2_password作为默认的身份验证插件,而不再是 mysql_native_password,但是客户端暂时不支持这个插件导致的. 解决方法一:修改MySQL全局配置文件 编辑 my.cnf文件,更改默认的身份认证插件. $ vi /etc/my.cnf 在 [mysqld]中添加下边的代码 default_authentication_plugin=mysql_native_password 然后重启mysql $ service my

The server requested authentication method unknown to the client

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client. In MySQL 8.0+, the default authentication plugin has changed from 'mysql_native_password' to 'caching_sha2_password', and the 'root'@'localhost' administrative a

mysql 8.0 错误The server requested authentication method unknown to the client

mysql 安装了最新版本8.0.11后创建用户并授权后,授权的用户连接数据库提示 The server requested authentication method unknown to the client 查阅一些相关的资料后发现是由于新版本的mysql账号密码解锁机制不一致导致的 解决办法: 删除创建的用户和授权, 找到mysql配置文件并加入 default_authentication_plugin=mysql_native_password 变为原来的验证方式,然后从新创建用户并

Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]报错解决方法

错误: 解决方法: 原文地址:https://www.cnblogs.com/NuoChong/p/10013272.html

PDO连接mysql8.0报PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers错误

安装mysql8.0之后,尝试使用php连接mysql,总是报PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers错误,网上找了很多资料,然而都没有多大用处. 查找了mysql官方说明文档才知道原来M8.0已经已经把默认字符集升级成ut8mb4了,于是找到my.cnf文件,修改如下: 1 [client] 2 port = 3306 3 socke

YII 错误 SQLSTATE[HY000] [2002] No such file or directory

在使用yii的yii\db\Connnection时发生错误 <?php namespace app\controllers; use yii\web\Controller; use yii\db\Connection; use Yii; class MyController extends Controller { public function actionIndex() { $country = Yii::$app -> db -> createCommand("sele

[HY000] [2054] php连接mysql时错误

会出现这样的原因,主要都是因为mysql8.0版本的缘故,8.0版本默认编码为utf8mb4 解决方法: MySQL配置文件my.ini中[mysqld]下加这两行 character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci 然后又出现了另一个问题:The server requested authentication method unknown to the client 也是由于mysql版本缘故,8.0版本my

PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can&#39;t connect to local MySQL server through socket &#39;MySQL&#39; (2)

如下所示,PHP连接MySQL报错: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2) 测试代码如下: <?php try { $dsn = 'mysql:dbname=php-note;host=localhost;port=3306;charset=utf8'; $username = 'root'; $password = 'root'; new PDO( $dsn,