在Linux
上 安装完MySQL
数据库后要对数据库进行字符集和校对集的修改(校对集是基于字符集) 可以通过SHOW VARIABLES LIKE '%char%';
和SHOW VARIABLES LIKE '%colla%';
来查看字符集和校对集
然后通过修改配置文件 来达到改变默认配置的目的 位于/etc/mysql/my.cnf
下有一个配置文件 里面有两个目录路径 分别是 /etc/mysql/conf.d
/etc/mysql/mysql.conf.d/
这么两个目录 那么我们改变编码可以是在当前这个my.cnf
改也可以去/etc/mysql/conf.d/
下的mysql.cnf
改变
注意 在修改的时候要暂停MySQL
的服务
-
service mysql status
查看状态 -
service mysql start
开启服务 -
service mysql stop
暂停服务 -
service mysql restart
重启服务
配置文件内容
# 客户端
[client]
default-character-set = utf8
# 服务端
[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
校对集 依赖 字符集
如果没有修改之前编码集是
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.20 sec)
校对集为
mysql> show variables like '%colla%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
改完之后的 编码集为
mysql> SHOW VARIABLES LIKE '%char%';
+--------------------------+----------------------------+
| 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)
校对集为
mysql> SHOW VARIABLES LIKE '%colla%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.01 sec)