问题
mysql在做查询条件时会出现乱码情况。
一、解决方法
1.登陆mysql查看mysql编码
mysql -u root -p #登陆
show variables like 'char%'; #查看MySQL编码,有两个不是utf8,而是latin1
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
2.配置mysql字符集
2.1配置mysqld.cnf文件
vi /etc/mysql/mysql.conf.d/mysqld.cnf
#在文件末尾添加代码
character-set-server=utf8
[mysqld]
...
...
character-set-server=utf8
2.2配置mysql.cnf文件
vi /etc/mysql/conf.d/mysql.cnf
#在文件末尾添加代码
[mysql]
default-character-set=utf8 #增加这个设置
3.重启mysql,重复步骤一、发现数据库字符集已改变
service mysql restart
注:在修改字符集之前创建的数据库,它使用的还是原来的字符集latin1
#只要左手百度,右手CSDN,我在运维码农的世界就不怕#