今天早上上班的時候,突然發現昨天好好的項目,今天報錯了,具體錯誤信息為:SQLSTATE[HY000] [2054] Server sent charset unknown to the client. Please, report to the developers
翻譯成中文為服務器向客戶端發送未知的字符集,向開發商報告,第一反應是數據庫字符集不對,檢查過后發現,字符集是正確的。經過思考,把連接數據庫配置文件中的localhost改成127.0.0.1,
問題解決。
這個問題多發生在mysql8.0的版本上,下面是網上的一些解決辦法:
ThinkPHP 3.2 連接MySQL 8時報如下錯誤:
SQLSTATE[HY000] [2054] Server sent charset unknown to the client. Please, report to the developers
網上查詢得知MySQL 8 默認字符集為utf8mb4,給出的解決方法都是設置MySQL的默認字符集為老版本的utf8,然而其實只需要在MySQL配置文件中[mysqld]下加這兩行
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
同時在TP配置文件中的數據庫連接信息中指明字符集utf8即可
'DB_CHARSET'=> 'utf8', // 字符集
如果又出現這樣的報錯:
QLSTATE[HY000] [2054] The server requested authentication method unknown to the client
是因為MySQL8中用戶的認證類型(Authentication type)默認為cacheing sha2 password導致的錯誤,需要修改用戶權限認證方式為5.x的認證方式
alter user 'root'@'%' identified with mysql_native_password by '123456';
#刷新權限
flush privileges;
然后在MySQL配置文件中[mysqld]下加上
default_authentication_plugin=mysql_native_password