CentOS7 修改 MySQL 字符集

使用 MySQL 时,为了让 MySQL 支持中文,需要把字符集修改为 UTF-8,方法如下:

1.查看 MySQL 字符集:
// 启动 MySQL
[root@localhost ~]# systemctl status mysqld.service
// 登录 MySQL
[root@localhost ~]# mysql -uroot -p
// 查看 MySQL 字符集
mysql>show variables like 'character_set%';

结果为:

+--------------------------+----------------------------+
| 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.修改 my.cnf 文件
// 退出 MySQL
mysql> \q
// 打开 my.cnf 文件
[root@localhost ~]# vim /etc/my.cnf

i 键进入 insert 模式
[mysqld] 字段中加入 character_set_server=utf8 ,如下:
在这里插入图片描述
Esc 键进入 command 模式,输入 :wq 保存文件并退出。

3.重启 MySQL,查看 MySQL 字符集
// 重启 MySQL
[root@localhost~]# service mysqld restart
// 登录 MySQL
[root@localhost ~]# mysql -uroot -p
// 查看字符集
mysql> show variables like 'character%';

显示如下:

+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

除了 character_set_filesystem,其它都为 UTF-8,完成!