The server requested authentication method unknown to the client

php连接MySQL8报错:

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

解决办法:

修改MySQL配置文件:C:\ProgramData\MySQL\MySQL Server 8.0\my.ini

# default_authentication_plugin=caching_sha2_password
default_authentication_plugin=mysql_native_password

登录到数据库,修改`mysql`.`user`表中的数据:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;

重启MySQL,PHP再次连接MySQL8就不会报错了。

实际上这个原因是PHP低版本不支持caching_sha2_password这种数据库认证方式造成的。