PHP連接MySQL 8.0報錯

Posted in :

在安裝Mysql8.0之後,需要跟我們原有的PHP+mysql 5.7 可以執行,升級到 mysql 8.0突然報錯。

報錯:

Server sent charset (255) unknown to the client. Please, report to the developers


原因:由於Mysql8.0將默認的字符集改為了utfmb4,因此和客戶端(不僅僅是PHP)的通信無法識別,需要更改my.cnf 或 mysql.conf.d/mysqld.cnf 來指定字符集。

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

報錯:

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';

針對已存在的用戶使用 ALTER USER 指令,後面的參數都用 create user 相同。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *