MySQL字符集相关变量介绍及binlog中字符集相关缺陷分析

MySQL支持多种字符集(character set)提供用户存储数据,同时允许用不同排序规则(collation)做比较。

本文基于MySQL5.7介绍了字符集相关变量的使用,通过例子描述了这些变量具体意义。分析了MySQL binlog中字符集相关处理的缺陷,这些缺陷会导致复制中断或者主备不一致。最后给出了修复上述缺陷的建议。

MySQL字符集相关基础知识介绍

character_set_system

character_set_system为元数据的字符集,即所有的元数据都使用同一个字符集。试想如果元数据采用不同字符集,INFORMATION_SCHEMA中的相关信息在不同行之间就很难展示。同时该字符集要能够支持多种语言,方便不同语言人群使用自己的语言命名database、table、column。MySQL选择UTF-8作为元数据编码,用源码固定。

  1. sql/mysqld.cc
  2. int mysqld_main(int argc, char **argv)
  3. {
  4. ...
  5. system_charset_info= &my_charset_utf8_general_ci;
  6. }
  1. > select @@global.character_set_system;
  2. +-------------------------------+
  3. | @@global.character_set_system |
  4. +-------------------------------+
  5. | utf8 |
  6. +-------------------------------+

MySQL会将identifier转换为system_charset_info(utf8)。

  1. sql/sql_lex.cc
  2. static int lex_one_token(YYSTYPE *yylval, THD *thd)
  3. {
  4. case MY_LEX_IDENT:
  5. ...
  6. lip->body_utf8_append_literal
  7. ...
  8. }
  9. void Lex_input_stream::body_utf8_append_literal(THD *thd,
  10. const LEX_STRING *txt,
  11. const CHARSET_INFO *txt_cs,
  12. const char *end_ptr)
  13. {
  14. ...
  15. if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
  16. {
  17. thd->convert_string(&utf_txt,
  18. &my_charset_utf8_general_ci,
  19. txt->str, txt->length,
  20. txt_cs);
  21. }
  22. else
  23. {
  24. utf_txt.str= txt->str;
  25. utf_txt.length= txt->length;
  26. }
  27. ...
  28. }
  29. sql/sql_yacc.yy
  30. IDENT_sys:
  31. IDENT { $$= $1; }
  32. | IDENT_QUOTED
  33. {
  34. THD *thd= YYTHD;
  35. if (thd->charset_is_system_charset)
  36. {
  37. ...
  38. }
  39. else
  40. {
  41. if (thd->convert_string(&$$, system_charset_info,
  42. $1.str, $1.length, thd->charset()))
  43. MYSQL_YYABORT;
  44. }
  45. }
  46. ;

character_set_server/collation_server

当create database没有指定charset/collation就会用character_set_server/collation_server,这两个变量可以动态设置,有session/global级别。

在源码中character_set_server/collation_server实际对应一个变量,因为一个collation对应着一个charset,所以源码中只记录CHARSET_INFO结构的collation_server即可。当修改character_set_server,会选择对应charset的默认collation。对于其他同时有charset和collation的变量,源码记录也都是记录collation。

  1. static Sys_var_struct Sys_character_set_server(
  2. "character_set_server", "The default character set",
  3. SESSION_VAR(collation_server), NO_CMD_LINE,
  4. offsetof(CHARSET_INFO, csname), DEFAULT(&default_charset_info),
  5. NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(check_charset_not_null));
  6. static Sys_var_struct Sys_collation_server(
  7. "collation_server", "The server default collation",
  8. SESSION_VAR(collation_server), NO_CMD_LINE,
  9. offsetof(CHARSET_INFO, name), DEFAULT(&default_charset_info),
  10. NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(check_collation_not_null));

通过下面case可以看到通过设置session中不同的character_set_server使创建database的默认charset和collation不同。

  1. > set character_set_server='utf8';
  2. > create database cs_test1;
  3. > select * from SCHEMATA where SCHEMA_NAME='cs_test1';
  4. +--------------+-------------+----------------------------+------------------------+----------+
  5. | CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
  6. +--------------+-------------+----------------------------+------------------------+----------+
  7. | def | cs_test1 | utf8 | utf8_general_ci | NULL |
  8. +--------------+-------------+----------------------------+------------------------+----------+
  9. > set character_set_server='latin1';
  10. > create database cs_test2;
  11. > select * from SCHEMATA where SCHEMA_NAME='cs_test2';
  12. +--------------+-------------+----------------------------+------------------------+----------+
  13. | CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
  14. +--------------+-------------+----------------------------+------------------------+----------+
  15. | def | cs_test2 | latin1 | latin1_swedish_ci | NULL |
  16. +--------------+-------------+----------------------------+------------------------+----------+

character_set_database/collation_database

该变量值session级别表示当前database的charset/collation,在后面的源码版本中该变量可能修正为只读,不建议修改该值。其global级别变量后面也会移除。

  1. > use cs_test1;
  2. > select @@character_set_database;
  3. +--------------------------+
  4. | @@character_set_database |
  5. +--------------------------+
  6. | utf8 |
  7. +--------------------------+
  8. > use cs_test2;
  9. > select @@character_set_database;
  10. +--------------------------+
  11. | @@character_set_database |
  12. +--------------------------+
  13. | latin1 |
  14. +--------------------------+

character_set_client

客户端发送到server的字符串使用的字符集,server会按照该变量值来解析客户端发来的语句。如果指定值和语句实际编码字符集不符就会解析出错,报语法错误或者得到非预期结果,例如下面的两个case。

  1. case1:实际使用utf8编码且包含中文字符,但设置character_set_clientlatin1
  2. > set character_set_client='latin1';
  3. > create table 字符集(c1 varchar(10));
  4. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '­—符集(c1 varchar(10))' at line 1
  5. > set character_set_client='utf8';
  6. > create table 字符集(c1 varchar(10));
  7. Query OK, 0 rows affected (0.14 sec)
  8. case2:实际使用utf8编码且包含中文字符,但设置character_set_clientgbk
  9. > create database cs_test;
  10. > use cs_test;
  11. > set character_set_client='gbk';
  12. > create table 收费(c1 varchar(10));
  13. > show tables;
  14. +-------------------+
  15. | Tables_in_cs_test |
  16. +-------------------+
  17. | 鏀惰垂 |
  18. +-------------------+
  19. > set character_set_client='utf8';
  20. > create table 收费(c1 varchar(10));
  21. > show tables;
  22. +-------------------+
  23. | Tables_in_cs_test |
  24. +-------------------+
  25. | 收费 |
  26. | 鏀惰垂 |
  27. +-------------------+
  28. 2 rows in set (0.00 sec)

character_set_connection/collation_connection

没有指定字符集的常量字符串使用时的字符集,例如下面两个case。

case1中当设置为utf8_general_ci比较时候忽略大小写,导致’a’=’A’结果为1,如果设置为utf8_bin不忽略大小写,’a’ = ‘A’的结果就是0。

case2中当设置character_set_connection为’latin1’的时候,’你好’ = ‘我好’返回结果为1,如果设置为’utf8’,返回结果就是0。设置为’latin1’返回结果为1的原因是utf8编码的中文字符是无法转换为latin1字符的。这里MySQL就把’你好’和’我好’都转换成了’??’。

case3中character_set_connection的不同导致create table语句中column的实际default value不同。

  1. case1:设置collation_connection是否忽略大小写导致结果不一致。
  2. > set collation_connection=utf8_general_ci;
  3. > select 'a' = 'A';
  4. +-----------+
  5. | 'a' = 'A' |
  6. +-----------+
  7. | 1 |
  8. +-----------+
  9. > set collation_connection=utf8_bin;
  10. > select 'a' = 'A';
  11. +-----------+
  12. | 'a' = 'A' |
  13. +-----------+
  14. | 0 |
  15. +-----------+
  16. case2:设置character_set_connection不同导致结果不一致。
  17. > set character_set_connection='latin1';
  18. Query OK, 0 rows affected (0.00 sec)
  19. > select '你好' = '我好';
  20. +---------------------+
  21. | '你好' = '我好' |
  22. +---------------------+
  23. | 1 |
  24. +---------------------+
  25. 1 row in set, 2 warnings (0.00 sec)
  26. > set character_set_connection='utf8';
  27. Query OK, 0 rows affected (0.00 sec)
  28. > select '你好' = '我好';
  29. +---------------------+
  30. | '你好' = '我好' |
  31. +---------------------+
  32. | 0 |
  33. +---------------------+
  34. > set character_set_connection='latin1';
  35. > select '你好';
  36. +----+
  37. | ?? |
  38. +----+
  39. | ?? |
  40. +----+
  41. case3:设置character_set_connection导致实际default value不同。
  42. > set character_set_connection='utf8';
  43. > create table cs_t(c1 varchar(10) default '你好')charset=utf8;
  44. > insert into cs_t values();
  45. > select * from cs_t;
  46. +--------+
  47. | c1 |
  48. +--------+
  49. | 你好 |
  50. +--------+
  51. > set character_set_connection='latin1';
  52. > create table cs_t1(c1 varchar(10) default '你好')charset=utf8;
  53. > insert into cs_t1 values();
  54. > select * from cs_t1;
  55. +------+
  56. | c1 |
  57. +------+
  58. | ?? |
  59. +------+

character_set_results

查询结果和错误信息的字符集,server会把返回给客户端的结果转换为对应字符集。例如下面case,当设置character_set_results为’latin1’的时候,会导致返回的中文变成’?’。

  1. > set character_set_results='utf8';
  2. > select '你好';
  3. +--------+
  4. | 你好 |
  5. +--------+
  6. | 你好 |
  7. +--------+
  8. > set character_set_results='latin1';
  9. > select '你好';
  10. +----+
  11. | ?? |
  12. +----+
  13. | ?? |
  14. +----+
  15. > create table cs_test(c1 varchar(10)) charset=utf8;
  16. > insert into cs_test values('你好'),('我好');
  17. > select * from cs_test;
  18. +------+
  19. | c1 |
  20. +------+
  21. | ?? |
  22. | ?? |
  23. +------+
  24. > set character_set_results='utf8';
  25. > select * from cs_test;
  26. +--------+
  27. | c1 |
  28. +--------+
  29. | 你好 |
  30. | 我好 |
  31. +--------+

binlog 中字符集相关缺陷

binlog当前字符集相关实现

对于很多DDL语句,binlog都是直接记录客户端发来的字符串,对于这些语句只要记录语句执行时候的环境变量就可以在备库正确执行。binlog中Query_log_event记录了character_set_client、collation_connection和collation_server,代码如下。记录这三个变量的原因读者可以参考前面各个变量的介绍和case。

  1. int THD::binlog_query(THD::enum_binlog_query_type qtype, const char *query_arg,
  2. size_t query_len, bool is_trans, bool direct,
  3. bool suppress_use, int errcode)
  4. {
  5. ...
  6. case THD::STMT_QUERY_TYPE:
  7. /*
  8. The MYSQL_BIN_LOG::write() function will set the STMT_END_F flag and
  9. flush the pending rows event if necessary.
  10. */
  11. {
  12. Query_log_event qinfo(this, query_arg, query_len, is_trans, direct,
  13. suppress_use, errcode);
  14. /*
  15. Binlog table maps will be irrelevant after a Query_log_event
  16. (they are just removed on the slave side) so after the query
  17. log event is written to the binary log, we pretend that no
  18. table maps were written.
  19. */
  20. int error= mysql_bin_log.write_event(&qinfo);
  21. binlog_table_maps= 0;
  22. DBUG_RETURN(error);
  23. }
  24. ...
  25. }
  26. Query_log_event::Query_log_event(THD* thd_arg, const char* query_arg,
  27. size_t query_length, bool using_trans,
  28. bool immediate, bool suppress_use,
  29. int errcode, bool ignore_cmd_internals)
  30. {
  31. ...
  32. int2store(charset, thd_arg->variables.character_set_client->number);
  33. int2store(charset+2, thd_arg->variables.collation_connection->number);
  34. int2store(charset+4, thd_arg->variables.collation_server->number);
  35. ...
  36. }

例如前面创建表cs_t1的case我们可以看到binlog如下。

  1. > set character_set_connection='latin1';
  2. > create table cs_t1(c1 varchar(10) default '你好')charset=utf8;
  3. SET TIMESTAMP=1516089074/*!*/;
  4. /*!\C utf8 *//*!*/;
  5. SET @@session.character_set_client=33,@@session.collation_connection=8,@@session.collation_server=8/*!*/;
  6. create table cs_t1(c1 varchar(10) default '你好')charset=utf8

binlog字符集相关缺陷

对于Query_log_event如果记录的query仅仅是客户端的输入,上面记录字符集变量的方法没有问题。但如果query是server内部生成或者拼接成的,上面直接从thread中获取变量值得方法就可能导致错误。

例如下面的testcase,这里为便于观察和理解case没有使用mysql-test方式,后面有mysql-test。这里主库执行成功,成功创建了表t和视图’收费明细表’,但备库在创建视图的时候却报语法错误。

  1. gbk编码写如下sql文本
  2. cs_test.sql
  3. use test;
  4. set @@session.character_set_client=gbk;
  5. set @@session.collation_connection=gbk_chinese_ci;
  6. create table t(c1 int);
  7. create view `收费明细表` as select * from t;
  8. 在主库执行
  9. > source path/cs_test.sql;
  10. > set character_set_results='gbk';
  11. > use test;
  12. > show tables;
  13. +----------------+
  14. | Tables_in_test |
  15. +----------------+
  16. | 收费明细表 |
  17. | t |
  18. +----------------+
  19. 备库
  20. > show slave status\G
  21. ...
  22. Last_SQL_Errno: 1064
  23. Last_SQL_Error: Error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`閺鎯板瀭閺勫海绮忕悰鈺? AS select * from t' at line 1' on query. Default database: 'test'. Query: 'CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `鏀惰垂鏄庣粏琛╜ AS select * from t'
  24. ...

缺陷分析,MySQL记录create view的binlog代码如下。由前面基础知识可以知道对于db、table这些元数据MySQL会先转换为system_charset_info(utf8)。因此在下面代码中append_identifier添加的table name为utf8编码的’收费明细表’,但是views->source.str又是client端原始的gbk编码方式,binlog_query记录的是thd中的character_set_client。即binlog中的query可能是由system_charset_info和character_set_client两种编码方式组成的字符串,记录的是当前character_set_client的值。

  1. sql/sql_view.cc
  2. bool mysql_create_view(THD *thd, TABLE_LIST *views,
  3. enum_view_create_mode mode)
  4. {
  5. ...
  6. if (views->db && views->db[0] &&
  7. (thd->db().str == NULL || strcmp(views->db, thd->db().str)))
  8. {
  9. append_identifier(thd, &buff, views->db,
  10. views->db_length);
  11. buff.append('.');
  12. }
  13. append_identifier(thd, &buff, views->table_name,
  14. views->table_name_length);
  15. if (lex->view_list.elements)
  16. {
  17. List_iterator_fast<LEX_STRING> names(lex->view_list);
  18. LEX_STRING *name;
  19. int i;
  20. for (i= 0; (name= names++); i++)
  21. {
  22. buff.append(i ? ", " : "(");
  23. append_identifier(thd, &buff, name->str, name->length);
  24. }
  25. buff.append(')');
  26. }
  27. buff.append(STRING_WITH_LEN(" AS "));
  28. buff.append(views->source.str, views->source.length);
  29. int errcode= query_error_code(thd, TRUE);
  30. thd->add_to_binlog_accessed_dbs(views->db);
  31. if (thd->binlog_query(THD::STMT_QUERY_TYPE,
  32. buff.ptr(), buff.length(), FALSE, FALSE, FALSE, errcode))
  33. res= TRUE;
  34. ...
  35. }

在MySQL源码中搜索binlog_query还可以找到多处类似的bug,可参考下面的testcase。

  1. --disable_warnings
  2. --source include/master-slave.inc
  3. --enable_warnings
  4. # case1:创建gbk编码中文名视图
  5. create table t(c1 int);
  6. SET @@session.character_set_client=gbk;
  7. set @@session.collation_connection=gbk_chinese_ci;
  8. set @@session.collation_server=utf8_general_ci;
  9. create view `收费明细` as select * from t;
  10. drop view `收费明细`;
  11. show tables;
  12. --sync_slave_with_master
  13. connection slave;
  14. show tables;
  15. connection master;
  16. drop table t;
  17. # case2:创建gbk编码中文名视图,且view body中包含中文
  18. connection master;
  19. SET @@session.character_set_client=gbk;
  20. create table 视图(c1 int);
  21. create view 视图信息 as select * from 视图;
  22. drop view 视图信息;
  23. # case3: drop table 语句会是generated by server.
  24. drop table 视图;
  25. --sync_slave_with_master
  26. # case4:内存表,重启后再次访问时会生成delete from tableName语句.
  27. connection master;
  28. SET @@session.character_set_client=utf8;
  29. set @@session.collation_connection=utf8_general_ci;
  30. set @@session.collation_server=utf8_general_ci;
  31. create table `收费明细表`(c1 int) engine=memory;
  32. create view tv as select * from `收费明细表`;
  33. --connection slave
  34. -- source include/stop_slave.inc
  35. --let $rpl_server_number= 1
  36. --source include/rpl_restart_server.inc
  37. # access memory table after restarting server cause binlog 'delete from tableName'
  38. connection master;
  39. SET @@session.character_set_client=gbk;
  40. set @@session.collation_connection=gbk_chinese_ci;
  41. set @@session.collation_server=utf8_general_ci;
  42. select * from tv;
  43. --connection slave
  44. -- source include/start_slave.inc
  45. connection master;
  46. --sync_slave_with_master
  47. connection slave;
  48. # case5:character_set_client为gbk时中文名的procedure
  49. connection master;
  50. delimiter $$;
  51. create procedure 收费明细()
  52. begin
  53. select 'hello world';
  54. end $$
  55. delimiter ;$$
  56. drop procedure `收费明细`;
  57. connection master;
  58. SET @@session.character_set_client=utf8;
  59. set @@session.collation_connection=utf8_general_ci;
  60. set @@session.collation_server=utf8_general_ci;
  61. drop view tv;
  62. drop table `收费明细表`;
  63. --sync_slave_with_master
  64. connection slave;
  65. show tables;
  66. # case6: 不同环境变量下create table like/as 表中有中文default value的
  67. set character_set_client = utf8;
  68. set character_set_connection = utf8;
  69. set character_set_database = utf8;
  70. set character_set_results = utf8;
  71. set character_set_server = utf8;
  72. CREATE TABLE `t1` (
  73. `id` int(11) NOT NULL,
  74. `orderType` char(6) NOT NULL DEFAULT '已创建',
  75. PRIMARY KEY (`id`)
  76. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  77. create temporary table `tm` (c1 varchar(10) default '你好');
  78. show create table t1;
  79. ## switch client charset
  80. set character_set_client = latin1;
  81. set character_set_connection = latin1;
  82. set collation_server = utf8_bin;
  83. CREATE TABLE t2 SELECT * FROM t1;
  84. create table t3 like tm;
  85. show create table t2;
  86. show create table t3;
  87. --sync_slave_with_master
  88. connection slave;
  89. show tables;
  90. set character_set_client = utf8;
  91. set character_set_connection = utf8;
  92. set character_set_database = utf8;
  93. set character_set_results = utf8;
  94. set character_set_server = utf8;
  95. show create table t1;
  96. show create table t2;
  97. show create table t3;
  98. connection master;
  99. drop table t1;
  100. drop table t2;
  101. drop table t3;
  102. --sync_slave_with_master
  103. --source include/rpl_end.inc

修复方法

对于create view/create procedure等一个query包含两种编码的可以将system_charset_info的部分转换为thread中的character_set_client。这里的转换需要考虑当前thread中character_set_client不支持utf8字符的问题,当转换失败需要报错,否则主备会不一致。

对于完全由server生成的query,例如delete from和drop table语句,其query实际可以理解为system_charset_info,这种语句可以直接使binlog中character_set_client部分记录system_charset_info,而不是thread中的变量值。

该bug在MariaDB中也存在,可以见MDEV-14249,参考链接中的fix diff或者MariaDB的修复。