1.首先,通过以下命令进入mysql
[root@VM_168_4_centos ~]# cd /
[root@VM_168_4_centos /]# cd usr/etc
[root@VM_168_4_centos etc]# mysql
2.然后查看mysql的字符集编码
mysql>show variables like 'character_set%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
3.然后进入etc下的my.cnf文件
cd etc
vim my.cnf
找到如下代码
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
4.修改文件,在 [mysqld_safe] 的上面粘贴如下三行,然后保存
character-set-server=utf8
[mysql]
default-character-set=utf8
5.重启mysql服务:service mysqld restart
再次查看编码
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+