在MySQl里面,字符集转换是个令人头痛的问题,现在小编为大家总结下更换MySQL数据库字符集的那些事儿。
一、MySQL应用的character_set变量:
root@test 09:22:42>SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /u01/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
二、MySQL修正collation变量:
root@test 07:46:23>SHOW VARIABLES LIKE 'collation%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | gbk_chinese_ci |
| collation_server | gbk_chinese_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
三、从客户端到服务器,然后再到客户端所经历的编码转换过程:
query -> _client (客户端准备发送query到网络的编码)
| (network: client -> network)
\|/
_connection (写网络转化后的编码, 一般都会和client一致)
| (network -> server)
_server & collation_server (服务器转换后的编码)
| (server -> mysql server)
\|/
_database & collation_database (数据库转换后的编码)
| (mysql server)
\|/
_table/_filed ... (数据库内的编码转换)
| (mysql server)
\|/
_filesystem (文件系统层的编码)
\|/
_database (服务器数据库转换后的编码)
| (mysql server)
\|/
_results (查询结果集转化后的编码)
| (mysql server)
\|/
_connection (服务器发送前转化后的编码)
| (network sending)
\|/
result
四、常用的2条set命令有何具体的意义呢,如下解释下:
A SET NAMES 'x' 等效于下面三条语句:
SET character_set_client = x;
SET character_set_results = x;
SET character_set_connection = x;
A SET CHARACTER SET 'x' 等效于下面三条语句:
SET character_set_client = x;
SET character_set_results = x;
SET collation_connection = @@collation_database;
以上都是介绍编码的字符集,那么线上如何转化想要的字符集:
1. 按所需的字符集编码格式导出数据,然后按所需的字符集编码格式导入到数据库中,这是许多DBA推荐的方法。
2. 尝试在结果集/客户端转换,这个需要字符集之间兼容。
领取专属 10元无门槛券
私享最新 技术干货