联系QQ 284710375
首页 > 技术分享 > MySQL
收藏

登录MySQL提示mysqli::real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]2024-08-15 09:29:06

大潇博客 原创文章,转载请标明出处

一、版本:MySQL8.4


二、问题

在Linux的shell下输入mysql -u root -p,再输入密码可以正常登录,切换到其它端,比如phpMyAdmin,会报错:mysqli::real_connect():Theserverrequestedauthenticationmethodunknowntotheclient[caching_sha2_password]

如下图



三、原因

从8.0.11版本起,caching_sha2_password是默认的身份验证插件,而不是以往的mysql_native_password。

说明:caching_sha2_password和sha256_password认证插件比mysql_native_password插件提供的密码加密更加安全,并且caching_sha2_password加密比sha256_password的加密性能更好。由于caching_sha2_password这样优秀的安全和性能特性,让他作为MySQL8.0的首选认证插件,这也是默认的认证插件插件而不是mysql_native_password。


四、解决方法

1、将加密方式改为旧的

MySQL8以上版本,在配置文件my.cnf的[mysqld]下添加:

[mysqld]

default_authentication_plugin=mysql_native_password


2、改为旧的加密方式,并将用户也进行相应的更改

在MySQL8.4中,像方法1那样做,会报错并无法正常启动MySQL,查看文档发现“In MySQL 8.4, the mysql_native_password server-side plugin is disabled by default”,说明MySQL8.4已经不允许这样配置。

但文档给出了另一种方案,在配置文件my.cnf中添加:

mysql_native_password=ON

如果不做上面的设置,在修改账号验证方式时会报错:ERROR 1524 (HY000): Plugin 'mysql_native_password' is not loaded

然后重启MySQL,在shell中登录MySQL,做如下配置:

mysql -uroot -p

alter user 'root'@'localhost' identified with mysql_native_password;

alter user 'root'@'localhost' identified by '123456';


3、使用支持新的加密方式的客户端(Client),比如等于或高于8.0.4版本的libmysqlclient

4、使用支持新的加密方式的连接驱动(Connector)


打赏

阅读排行

大家都在搜

博客维护不易,感谢你的肯定
扫码打赏,建议金额1-10元
  • 15601023311