新安装了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;
网友评论