php连接mysql报错: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

一,报错信息:

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

二,phpmyadmin也报错:

image

三,原因:

mysqlnd的插件中缺少

Loaded plugins => mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password 

一种办法是升级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:

image

四,解决:

改用php7.4以上,(将 PHP 升级至 ‌7.4.4 或更高版本‌(如 PHP 7.4.4+),可直接解决兼容性问题)

一个重点:编译php时需要把openssl参数带上

安装完成后mysqlnd的信息中显示包含auth_plugin_caching_sha2_password,表示成功

mysqlnd

mysqlnd => enabled
Version => mysqlnd 7.4.33
Compression => supported
core SSL => supported
extended SSL => supported
Command buffer size => 4096
Read buffer size => 32768
Read timeout => 86400
Collecting statistics => Yes
Collecting memory statistics => No
Tracing => n/a
Loaded plugins => mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password,auth_plugin_caching_sha2_password,auth_plugin_sha256_password
API Extensions => pdo_mysql,mysqli 

 

posted @ 2026-01-10 10:21  刘宏缔的架构森林  阅读(11)  评论(0)    收藏  举报