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: 1267Last_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=0SET 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=0use `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=0SET 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 ...
随机推荐
- CSS3学习系列之布局样式(一)
多栏布局 使用float属性或position属性的缺点: 使用float属性或position属性进行页面中的简单布局的例子. <!DOCTYPE html> <html lang ...
- mybatis学习笔记(一)-- 简单入门(附测试Demo详细过程)
写在最前 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生M ...
- 今天真开心,终于知道怎么打包apk了
1.函数上下文的产生,并不是函数定义时决定的,而是函数调用时产生的:来个栗子 function creep(){return this;} console.log(creep()) var sneak ...
- jquery.validata.js 插件
一.Validate插件描述 Validate是基于jQuery的一款轻量级验证插件,内置丰富的验证规则,还有灵活的自定义规则接口,HTML.CSS与JS之间的低耦合能让您自由布局和丰富样式,支持in ...
- gulp使用流程
1.全局安装gulp $ npm install --global gulp 2.作为项目的开发依赖(devDependencies)安装 $ npm install --save-dev gulp ...
- JS - 实现简单易用的倒计时 x 天 x 时 x 分 x 秒
<script> (function () { var tian = document.getElementsByClassName('JS-tian')[0]; var shi = do ...
- Ubuntu14.04设置开机自启动程序
启动应用程序可以帮助我们选择开机启动项.但是在Ubuntu14.04通过Dash输入startup 找不到启动应用程序了,可以通过在控制台输入以下内容: gnome-session-propertie ...
- SpringMVC原理及非注解配置详解
1. Spring介绍 Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单. 这些控制器 ...
- 如何开始使用bootstrap
登陆Bootstrap官网:http://getbootstrap.com/ Bootstrap中的JS插件依赖于jQuery,因此jQuery要在Bootstrap之前引用 bootstrap框架初 ...
- 关于tomcat下startup.bat双击闪退的问题
背景:之前做单点登录,复制了几个tomcat,改了各自端口,当做不同服务器用. 今天无意间随便点击了一个tomcat下的startup.bat批处理文件,结果出来控制台,没出几行信息就闪退了.点击其他 ...