PHP與Mysql8不兼容問題彙總

安裝Mysql8.0之後,需要跟我們原有的PHP進行協同工作,然而原先與Mysql5.1能夠很好協同的代碼,突然報錯,看來需要做一些額外的工作。

報錯:PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers

根據網上資料顯示,是由於Mysql8.0將默認的字符集改爲了utfmb4,因此和客戶端(不僅僅是PHP)的通信無法識別,我們需要更改my.cnf來指定字符集。

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8

報錯:PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]

根據網上資料顯示,是由於用戶身份認證的加密方式不兼容導致的,mysql8.0中默認方式爲caching_sha2_password,引起老版本兼容性問題,老版本加密方式爲mysql_native_password

新建用老版加密方式初始化密碼的用戶即可:

CREATE USER username@localhost identified with mysql_native_password by 'password';

報錯:Access denied for user 'root'@'localhost' (using password: YES)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'oss'@'%';
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

在我給其他用戶加權限的時候,報錯無權限,原因是我一不小心刪掉了root身份的用戶,雖然網上有很多的文檔解決這個問題,但是我重建後的root用戶雖然擁有Grant_priv: Y但依然無法成功分配權限,我很頭疼。

解決方法:重裝,參考文章安裝Mysql8.0

總結

mysql8.0有什麼新的特性我沒有詳細查看文檔,但是兼容性先讓我吃了一頓苦頭,還好在解決完這3個問題後,我的PHP程序成功跑了起來,下面我要去升級PHP5.1PHP7了。

參考資料

  1. PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers:https://stackoverflow.com/que...
  2. PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]:https://stackoverflow.com/que...
  3. 安裝Mysql8.0:https://segmentfault.com/a/11...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章