解决方案:通过show variables like '%char%';查询到客户端字符集,数据库字符集,服务器字符集都不是utf8,我的mysql5.7通过rpm安装,找到mysql的配置文件/etc/my.cnf,进行如下配置
1、在[client]字段里加入default-character-set=utf8,如下:
[html] view plain copy
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
2、在[mysqld]字段里加入character-set-server=utf8,如下:
[html] view plain copy
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8
3、在[mysql]字段里加入default-character-set=utf8,如下:
[html] view plain copy
[mysql]
no-auto-rehash
default-character-set=utf8
然后通过service mysqld restart重启数据库
使用SHOW VARIABLES LIKE ‘character%’;查看,发现数据库编码全已改成utf8。
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+