error 1270 mysql主從

psufnxk2000發表於2017-04-07
作者:河南-老宋(志強)
連結:https://zhuanlan.zhihu.com/p/26224566
來源:知乎
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

現象:

mysql 5.6

主從報下面的錯誤:

mysql> show slave status \G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: ****

                  Master_User: ucloudbackup

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000003

          Read_Master_Log_Pos: 43347

               Relay_Log_File: mysql-relay.000002

                Relay_Log_Pos: 1810

        Relay_Master_Log_File: mysql-bin.000003

             Slave_IO_Running: Yes

            Slave_SQL_Running: No

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 1270

                   Last_Error: Error 'Illegal mix of collations (utf8_general_ci,COERCIBLE), (latin1_swedish_ci,IMPLICIT), (utf8_general_ci,COERCIBLE) for operation 'concat'' on query. Default database: 'test'. Query: 'INSERT INTO test (addressType, addressName)  VALUES ('sh_jincheng', concat('上海市', NAME_CONST('i',180),'室'))'

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 1647

              Relay_Log_Space: 43710

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: NULL

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 1270

               Last_SQL_Error: Error 'Illegal mix of collations (utf8_general_ci,COERCIBLE), (latin1_swedish_ci,IMPLICIT), (utf8_general_ci,COERCIBLE) for operation 'concat'' on query. Default database: 'test'. Query: 'INSERT INTO test (addressType, addressName)  VALUES ('sh_jincheng', concat('上海市', NAME_CONST('i',180),'室'))'

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 169052043

                  Master_UUID: a2b11ba1-1552-11e7-a33b-52540059bf44

             Master_Info_File: /opt/udb/instance/mysql-5.6/2f2a1aea-b636-48b7-9006-a1af025d90d0/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State:

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp: 170330 22:19:10

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set: a2b11ba1-1552-11e7-a33b-52540059bf44:1-105

            Executed_Gtid_Set: a2b11ba1-1552-11e7-a33b-52540059bf44:1-5

                Auto_Position: 0

1 row in set (0.00 sec) 

原來有一個筆記 ,裡面寫到:

ERROR 1267 concat name_const函式



今天朋友他們有一個錯誤,最後是因為:

MySQL> select concat('¥',NAME_CONST('tttt',1234));

ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation 'concat'

這個類似的錯誤原來也見過:那是因為兩表關聯時,關聯的欄位是有索引的,但是索引不是按的相同的COLLATION,所以出現的問題,修改為一樣之後,即可正常。

又試了一下

MySQL> select concat('a',NAME_CONST('tttt',1234));

+-------------------------------------+

| concat('a',NAME_CONST('tttt',1234)) |

+-------------------------------------+

| a1234                               |

+-------------------------------------+

1 row in set (0.00 sec)

MySQL> select concat('工',NAME_CONST('tttt',1234));

ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation 'concat'



從這裡看,用漢字的類的就不正常。



MySQL> select COLLATION('工');

+------------------+

| COLLATION('工')  |

+------------------+

| utf8_general_ci  |

+------------------+

1 row in set (0.00 sec)

MySQL> select COLLATION('a');

+-----------------+

| COLLATION('a')  |

+-----------------+

| utf8_general_ci |

+-----------------+

1 row in set (0.00 sec)

MySQL> select COLLATION('¥');

+------------------+

| COLLATION('¥')  |

+------------------+

| utf8_general_ci  |

+------------------+

1 row in set (0.00 sec)

MySQL> select COLLATION(NAME_CONST('tttt',1234));

+------------------------------------+

| COLLATION(NAME_CONST('tttt',1234)) |

+------------------------------------+

| binary                             |

+------------------------------------+

1 row in set (0.00 sec)



有點暈了,前前看 'a','工','¥' 都是一樣的, 漢字的不行。



看看這個函式:

This function is for internal use only,可以寫成別的方式 

比如這裡的 

MySQL> select NAME_CONST('tttt',1234);

+------+

| tttt |

+------+

| 1234 |

+------+

1 row in set (0.00 sec)

可以寫為:

MySQL> select 1234 as 'tttt';

+------+

| tttt |

+------+

| 1234 |

+------+

1 row in set (0.00 sec)

一樣的效果 。



另:

MySQL> select concat('¥',NAME_CONST('tttt','1234'));

+-----------------------------------------+

| concat('¥',NAME_CONST('tttt','1234'))  |

+-----------------------------------------+

| ¥1234                                  |

+-----------------------------------------+

這樣也不會報錯:


不知道開發到底是什麼 意思 ,和開發商量 才好最後約定怎麼搞。 

當時朋友遇到這個問題,最後他們也沒有反饋這個是怎麼回事,我也就忘記了這個事情,這次是自己遇到這個問題,並且分析問題時較之前也多了一些方法,這次就從原始碼看下這個問題。

看了幾分鐘程式碼,有一個懷疑點是 儲存過程的呼叫引起的這個問題(是在subst_spvars函式中完成的,這裡就不列出來程式碼了),就去主庫檢視,果然有這個procedure,


重現方法:

在主庫上建表:

主庫Binlog_format=mixed;


CREATE TABLE `test` (

`addressId` int(11) NOT NULL AUTO_INCREMENT,

`addressType` varchar(30) NOT NULL,

`addressName` varchar(300) NOT NULL,

`usable` tinyint(4) NOT NULL DEFAULT '1',

PRIMARY KEY (`addressId`)

) ENGINE=InnoDB AUTO_INCREMENT=202 DEFAULT CHARSET=utf8mb4



在主庫上建procedure:

delimiter //

CREATE  PROCEDURE `test_insert`()
BEGIN
DECLARE i INT DEFAULT 180;# can not be 0
WHILE i<400
DO
INSERT INTO test (addressType, addressName)  VALUES ('sh_jincheng', concat('上海市',i,'室'));
SET i=i+1;
END WHILE ;
commit;
END
//

delimiter ; 

呼叫procedure:

mysql> call test_insert();

Query OK, 0 rows affected (0.21 sec)

從庫上就會報上面的錯.

解決方法:

可以把 binlog_format=row 解決這個問題

宣告:原始碼新手,僅代表個人意見,非權威,如果誤導看官,見諒。

QQ 273002188 歡迎一起學習
QQ 群 236941212
oracle,mysql,PG 相互交流

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25099483/viewspace-2136800/,如需轉載,請註明出處,否則將追究法律責任。

相關文章