使用PHP鏈接MySQL 8的時候,可能會發生如標題所示的錯誤:php
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
發生這種錯誤,是因爲MySQL 8默認使用了新的密碼驗證插件:caching_sha2_password,而以前的PHP版本中所帶的mysqlnd沒法支持這種驗證。解決這個問題,有兩種辦法。mysql
PHP 7.2.8和PHP 7.1.20已經能夠支持caching_sha2_password,直接鏈接MySQL 8。sql
截止PHP 7.0.31和PHP 5.6.37還沒法支持caching_sha2_password,不知道後續版本是否會作出支持。函數
能夠經過phpinfo()函數了解當前安裝的PHP是否支持caching_sha2_password:插件
CREATE USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘password!2#4‘;
ALTER USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password
或server
ALTER USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘new_password‘;
採用前一種方式,帳戶的密碼將被清除;BY子句將爲帳戶設置新的密碼。io
# default-authentication-plugin=mysql_native_password
請刪除註釋符號「#」並從新啓動mysqld使之生效,此後建立的帳戶均默認使用mysql_native_password。cli