MySQL replication illegal mix of collations
MySQL replication case 一则
转载:http://www.vmcd.org/2013/09/mysql-replication-case-%E4%B8%80%E5%88%99/
Posted by admin on September 10th, 2013
最近同事处理了一则mysql复制错误.发出来参考下
MYSQL同步出错,报错信息如下:
Last_Errno: 1267 Last_Error: Error 'Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation 'concat'' on query. Default database: 'inshop_app'. Query: 'INSERT INTO inshop_app.app_sms_order_info (ORDER_CODE, ORDER_TIME, ORDER_AMOUNT, ORDER_CONTENT, BUY_NUM, ORDER_STATUS, MERCHANT_ID, CREATE_TIME, UPDATE_TIME, APP_TYPE, pay_time, remark) VALUES( NAME_CONST('my_order_code',_utf8'SBY130830010708_F0' COLLATE 'utf8_general_ci'),NOW(),'0','1次', NAME_CONST('my_sms_num',1125000),'1', NAME_CONST('my_merchant_id',10708),NOW(),NOW(),'2',NOW(),CONCAT ('钻展赠送:', NAME_CONST('my_sms_num',1125000)))' |
出错原因分析:
此SQL在Master上执行时是这样的
INSERT INTO inshop_app.app_sms_order_info (ORDER_CODE, ORDER_TIME, ORDER_AMOUNT, ORDER_CONTENT, BUY_NUM, ORDER_STATUS, MERCHANT_ID, CREATE_TIME, UPDATE_TIME, APP_TYPE, pay_time, remark) VALUES( 'SBY130830010708_F0',NOW(),'0','1次', 1125000,'1', 10708,NOW(),NOW(),'2',NOW(),CONCAT ('钻展赠送:', 1125000)) |
该SQL本身是没问题的,执行成功,但是MYSQL在记录BINLOG的时候,会对常量用NAME_CONST()函数进行“标识”
同步的报错就出现在这个地方
CONCAT ('钻展赠送:', NAME_CONST('my_sms_num',1125000)) |
其中,’钻展赠送:’是UTF8字符集,NAME_CONST(‘my_sms_num’,1125000)得到的数值型常量被自动转型为LATIN1字符集,外层的CONCAT()函数不支持二种不同字符集进行连接,于是报错
以下测试可验证此分析:
无NAME_CONST()函数标识常量时,即如同在Master上执行时,成功
09:29:06 inshop_app> select concat('钻展赠送',123); +----------------------------+ | concat('钻展赠送',123) | +----------------------------+ | 钻展赠送123 | +----------------------------+ 1 row in set (0.00 sec) |
有NAME_CONST()函数标识常量时,即如同在Slave上执行时,失败
09:25:17 inshop_app> select concat('钻展赠送',name_const('colname',123)); ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation 'concat' |
报错与同步是一样的错误
什么情况下MySQL会自动加上NAME_CONST函数
测试1: 直接insert
11:27:32 test> insert into lengzhenguo_mha(c3,c4) values(1,'a'),('2','b'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 |
BINLOG中的内容
#130909 11:28:35 server id 2009 end_log_pos 469 Query thread_id=10 exec_time=0 error_code=0 SET TIMESTAMP=1378697315/*!*/; insert into lengzhenguo_mha(c3,c4) values(1,'a'),('2','b') /*!*/; |
测试2: 简单的存储过程
13:16:42 test> create procedure p_test() -> begin -> insert into lengzhenguo_mha(c3,c4) values(10,'abc'),('20','xyz'); -> commit; -> end -> $ Query OK, 0 rows affected (0.00 sec) 13:17:38 test> call p_test(); Query OK, 0 rows affected (0.00 sec) |
BINLOG中的内容
#130909 13:18:21 server id 2009 end_log_pos 328 Query thread_id=12 exec_time=0 error_code=0 use `test`/*!*/; SET TIMESTAMP=1378703901/*!*/; insert into lengzhenguo_mha(c3,c4) values(10,'abc'),('20','xyz') /*!*/; |
测试3:带参数的存储过程 类似bind value
13:22:43 test> CREATE procedure p_test_2 (i bigint, j varchar(30)) -> begin -> insert into lengzhenguo_mha(c3,c4) values(i,j); -> commit; -> end -> $ Query OK, 0 rows affected (0.00 sec) 13:23:16 test> call p_test_2(100,'dba'); Query OK, 0 rows affected (0.00 sec) 13:25:10 test> call p_test_2('500','dba'); Query OK, 0 rows affected (0.00 sec) |
BINLOG中的内容
#130909 13:23:32 server id 2009 end_log_pos 612 Query thread_id=12 exec_time=0 error_code=0
SET TIMESTAMP=1378704212/*!*/; insert into lengzhenguo_mha(c3,c4) values( NAME_CONST('i',100), NAME_CONST('j',_latin1'dba' COLLATE 'latin1_swedish_ci')) /*!*/; #130909 13:25:15 server id 2009 end_log_pos 1226 Query thread_id=12 exec_time=0 error_code=0 SET TIMESTAMP=1378704315/*!*/; insert into lengzhenguo_mha(c3,c4) values( NAME_CONST('i',500), NAME_CONST('j',_latin1'dba' COLLATE 'latin1_swedish_ci')) /*!*/; |
注意:’500′在写入Binlog时,已经被转换成数值型了
目前已知的解决方法:
方法1:不要直接使用数值,直接给予字符串,建议使用此方法
09:25:27 inshop_app> select concat('钻展赠送',name_const('colname','123')); +----------------------------------------------------+ | concat('钻展赠送',name_const('colname','123')) | +----------------------------------------------------+ | 钻展赠送123 | +----------------------------------------------------+ 1 row in set (0.00 sec) |
注意:这里的123加引号,字符串~
方法2:先进行类型转换
09:56:32 inshop_app> select concat('钻展赠送',convert(name_const('colname',123) using utf8)); +----------------------------------------------------------------------+ | concat('钻展赠送',convert(name_const('colname',123) using utf8)) | +----------------------------------------------------------------------+ | 钻展赠送123 | +----------------------------------------------------------------------+ 1 row in set (0.00 sec) |
MySQL replication illegal mix of collations的更多相关文章
- 彻底解决phpcms v9升级后,文章发布出现: Mysql 1267错误:MySQL Error : Illegal mix of collations 解决办法
彻底解决phpcms v9升级后,文章发布出现: MySQL Query : SELECT * FROM `withli_a`.`v9_keyword` WHERE `keyword` = '吼吼' ...
- MySQL Error: Illegal mix of collations for operation 'concat'
在使用concat连接字符串时出现错误:MySQL Error: Illegal mix of collations for operation 'concat' 原因:字段操作默认为UTF8的编码, ...
- 【MySQL】Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and ...
线上遇到这个问题,详细信息如下: SQL state [HY000]; error code [1267]; Illegal mix of collations (utf8mb4_general_ci ...
- mysql提示:Illegal mix of collations for operation ‘UNION’
http://www.111cn.net/database/mysql/56096.htm show variables like "%char%"; show variables ...
- Mysql Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
MySQL字符串比较bug: select * from table_a a left join table_b b on a.field_a = b.field_b error: Illegal ...
- mysql调用存储过程出现Illegal mix of collations错误
执行sql语句正常 执行存储过程 异常 提示 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMP ...
- mysql 客户端连接报错Illegal mix of collations for operation
服务端用的是utf-8,客户端工具打开表和视图是会报Illegal mix of collations for operation错误,经排查,可以采用以下语句解决 SET character_set ...
- java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '=' 异常处理,及MySQL数据库编码设置
java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,C ...
- 【mysql】【转发】[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,I
[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for o ...
随机推荐
- Maven pom.xml配置详解
POM的全称是“ProjectObjectModel(项目对象模型)”. 声明规范 <projectxmlns="http://maven.apache.org/POM/4.0.0&q ...
- Python 3从入门到精通01-环境搭建
本系列开始介绍Python3的基础教程,为什么要选中Python 3呢?因为最近看到一些资料和课程,都是Python 3授课的,例如,大数据,机器学习,数据挖掘等等:还有一个目的,我想彻底地,系统地学 ...
- ReactiveSwift源码解析(七) Signal的CombineLatest的代码实现
本篇博客我们就来聊一下combineLatest()的使用以及具体的实现方式.在之前的<iOS开发之ReactiveCocoa下的MVVM>的博客中我们已经聊过combineLatest( ...
- springmvc(三) 参数绑定、
前面两章就介绍了什么是springmvc,springmvc的框架原理,并且会简单的使用springmvc以及ssm的整合,从这一章节来看,就开始讲解springmvc的各种功能实现,慢慢消化 --W ...
- Android实现模拟表单上传
很久以前,写过一篇关于下载的文章:基于HTTP协议的下载功能实现,今天对于Android上的文件上传,也简单的提两笔.在Android上,一般使用Http 模拟表单或者FTP来进行文件上传,使用FTP ...
- webgl自学笔记——光照
在Webgl中我们使用顶点着色器和片元着色器来为我们的场景创建光照模型.着色器允许我们使用数学模型来控制如何照亮我们的场景. 最好有线性代数的相关知识. 本章中: 光源.法线.材料 光照和着色的区别 ...
- JavaScript用typeof判断变量是数组还是对象,都返回object
在JavaScript中所有数据类型严格意义上都是对象,但实际使用中我们还是有类型之分,如果要判断一个变量是数组还是对象使用typeof搞不定,因为它全都返回object. 使用typeof加leng ...
- <经验杂谈>Mysql中字符串处理的几种处理方法concat、concat_ws、group_concat
Mysql中字符串处理的几种处理方法concat.concat_ws.group_concat以下详情: MySQL中concat函数使用方法:CONCAT(str1,str2,-) 返回结果为连接参 ...
- Java设计模式之模板方法设计模式(银行计息案例)
不知道为什么,这几天对Java中的设计模式非常感兴趣,恰巧呢这几天公司的开发任务还不算太多,趁着有时间昨天又把模板方法模式深入学习了一下,做了一个客户在不同银行计息的小案例,感触颇深,今天给各位分享一 ...
- 快速搞定selenium grid分布式
写这篇文章,似乎有点重复造轮子的嫌疑.当看了几篇相关文章后,我还是决定把半年前的半成品给完成了. 以传统的方式部署分布式Selenium Grid集群需要耗费大量时间和机器成本来准备测试环境. Sna ...