SQLSTATE[HY000] [2054] The server requested authentication method unknown to client
报个此错误是由于php7.3版本的mysqlnd扩展没有mysql8 的 密码认证方式 caching_sha2_password;
查看一下php的phpinfo关于mysqlnd扩展的信息:
$ php73/php/bin/php --ri mysqlnd
mysqlnd => enabled
Version => mysqlnd 5.0.12-dev - 20150407 - $Id: 7cc7cc96e675f6d72e5cf0f267f48e167c2abb23 $
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_sha256_password
API Extensions => pdo_mysql,mysqli
可以看到 Loaded plugins 插件里没有caching_sha2_password方法。
然而mysql8 默认的密码认证方式采用 caching_sha2_password。
那怎么破解这个难题呢?
我们把mysql8 的默认密码认证方式改成mysql_native_password,因为php大部分版本的pdo链接都支持此方式。
首先在初始化mysql数据库的时候 增加参数default_authentication_plugin设置
mysql/bin/mysqld --initialize --console\
--default_authentication_plugin=mysql_native_password\
--user=mysql
然后再,/etc/my.cnf 配置文件中也增加参数
……
[mysqld]
default_authentication_plugin=mysql_native_password
……
这样,mysql8 默认的密码认证方式就被改成了mysql_native_password
另外一种办法是,让php的mysqlnd扩展支持caching_sha2_password 认证方式,目前还没找到怎么让php支持它。
网上应该有资料,如果你有扩展它的办法,请记得发博客告诉大家。