相关推荐recommended
MySQL错误解决办法 SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
作者:mmseoamin日期:2024-03-20
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client [caching_sha2_password] in /usr/local/nginx/html/mysql.php:3 Stack trace: #0 /usr/local/nginx/html/mysql.php(3): PDO->__construct('mysql:host=127....', 'root', Object(SensitiveParameterValue)) #1 {main} thrown in /usr/local/nginx/html/mysql.php on line 3

错误原因

错误原因是对 MySQL 进行的版本升级,MySQL8中用户的认证类型(Authentication type)默认为 caching_sha2_password 导致的错误,需要修改用户权限认证方式为 mysql_native_password。

解决办法

1.数据库降级,退回以前版本

2. 改为 mysql_native_password 认证方式

  • 修改mysql配置文件/etc/my.cnf

    在[mysqld]节点下如下内容,如果以及存在default_authentication_plugin节点,覆盖即可。

    [mysqld]
    ...
    default_authentication_plugin=mysql_native_password
    ...
    

    由于更改了认证方式,所以要更改链接MYSQL用户的密码。以下以root为例

    # mysql -u root -p
    
    • 修改用户密码
      mysql> alter user 'root'@'%' identified with mysql_native_password by '123456';
      mysql> flush privileges;
      
      • 查看
        mysql> use mysql;
        mysql> select host,user,plugin from user;
        +-----------+------------------+-----------------------+
        | host      | user             | plugin                |
        +-----------+------------------+-----------------------+
        | %         | root             | mysql_native_password |
        | localhost | mysql.infoschema | caching_sha2_password |
        | localhost | mysql.session    | caching_sha2_password |
        | localhost | mysql.sys        | caching_sha2_password |
        +-----------+------------------+-----------------------+
        4 rows in set (0.00 sec)