美文网首页
关于mysql8.0版本和PHP7不兼容的问题

关于mysql8.0版本和PHP7不兼容的问题

作者: youngxs | 来源:发表于2019-08-20 10:55 被阅读0次

新安装了mysql8.0版本。当PHP连接数据库的时候,会出现一个情况就是连接数据库失败,一般来说会出现以下两种情况:

1.报错:PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers

2.报错:PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]

第一种出现这种情况主要是mysql8.0默认是utfmb4的格式,PHP默认的是UTF8格式,因此需要将mysql8.0默认格式设置为utf8,

就需要修改my.cnf,下面是我设置的my.cnf

[client]

default-character-set=utf8

[mysql]

default-character-set=utf8

[mysqld]

collation-server = utf8_unicode_ci

init-connect='SET NAMES utf8'

character-set-server = utf8

第二种情况,那是因为用户身份认证的加密方式不兼容导致的,mysql8.0中默认方式为caching_sha2_password,引起老版本兼容性问题,老版本加密方式为mysql_native_password。

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

CREATEUSERusername@localhostidentifiedwithmysql_native_passwordby'password';

然后在my.cnf中添加一行:default_authentication_plugin=mysql_native_password;

相关文章

网友评论

      本文标题:关于mysql8.0版本和PHP7不兼容的问题

      本文链接:https://www.haomeiwen.com/subject/oqmusctx.html