PHP与Mysql8不兼容问题汇总

38次阅读

共计 1367 个字符,预计需要花费 4 分钟才能阅读完成。

在安装 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.1 到 PHP7 了。
参考资料

PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers:https://stackoverflow.com/que…

PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]:https://stackoverflow.com/que…

安装 Mysql8.0:https://segmentfault.com/a/11…

正文完
 0