Mac环境下PHP连接mysql提示Server sent charset (255) unknown和(HY000/2054)
错误提示:
mysqli_connect(): Server sent charset (255) unknown to the client. Please, report to the developers
网友提供的解决办法(转载链接,感谢原著!):
https://www.jianshu.com/p/d5d8e825ebda
通过多次调试,发现我的MySQL配置文件my.cnf的配置参数在字符集方面没有问题,的确是utf8,后来发现配置文件中命令行"password = your_password"是被注销了的,解开注释后错误代码消失了,但却出现了另一行错误代码😂
mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES)
继续从网上搜罗大神们的解决办法,然鹅,又一次没能适用于我的问题,后来就瞎琢磨从php.ini文件里为mysql配置了如下代码(根据my.cnf),问题竟然解决了:
mysql.default_port = 3306
mysql.default_socket = /tmp/mysql.sock
mysql.default_user = root
mysql.default_password = 你自己的mysql密码
同时,保证my.cnf的配置如下就👌了,就是对应下列代码在php.ini里为mysql配置参数
[client]
default-character-set=utf8
#character-set-server=utf8(注意有网友推荐配置该行代码,我的结论是会报错,所以推荐使用上行代码)
password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server = utf8_general_ci
default_authentication_plugin=mysql_native_password
init_connect='SET NAMES utf8
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
init_connect='SET NAMES utf8'
最后,修改完参数记得重启MySQL和Apache哟,本人新进小白一只,欢迎广大码友们指正吐槽!