溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL從庫的列類型不一致導致的復制異常問題

發(fā)布時間:2020-07-23 03:48:38 來源:網(wǎng)絡 閱讀:3288 作者:我的二狗呢 欄目:MySQL數(shù)據(jù)庫

官方文檔:https://dev.mysql.com/doc/refman/5.6/en/replication-features-differing-tables.html


slave_type_conversions  這個參數(shù)在mysql5.5.3 引入,目的是啟用row 格式的bin-log 的時候,如果主從的column 的數(shù)據(jù)類型不一致,會導致復制失敗,mysql5.5.3 之后支持,主庫是int 從庫是bigint 這種類型的復制,

這個參數(shù)的意義就是控制些類型轉(zhuǎn)換容錯性。

 

如果從庫的字段類型范圍比主庫類型大,那么設(shè)置slave_type_conversions=ALL_NON_LOSSY后復制沒有問題的。

如果從庫類型比主庫類型小,比如從int 復制到tinyint ,雖然可以通過設(shè)置slave_type_conversions=ALL_LOSSY,ALL_NON_LOSSY讓主從不出問題,但是實際上會造成數(shù)據(jù)丟失的風險。

 

幾種值的設(shè)置:

    ALL_LOSSY允許數(shù)據(jù)截斷

    ALL_NON_LOSSY不允許數(shù)據(jù)截斷,如果從庫類型大于主庫類型,是可以復制的,反過了,就不行了,從庫報復制錯誤,復制終止。

    ALL_LOSSY,ALL_NON_LOSSY: 所有允許的轉(zhuǎn)換都會執(zhí)行,而不管是不是數(shù)據(jù)丟失。

    空值(不設(shè)置)要求主從庫的數(shù)據(jù)類型必須嚴格一致,否則都報錯。

Mode

Effect

ALL_LOSSY

In this mode, type conversions that would mean loss of information are  permitted.

This does not  imply that non-lossy conversions are permitted, merely that only cases  requiring either lossy conversions or no conversion at all are permitted; for  example, enabling only this mode permits an INT column to be converted to TINYINT (a lossy conversion), but not a TINYINT column to an INT column (non-lossy). Attempting  the latter conversion in this case would cause replication to stop with an  error on the slave.

ALL_NON_LOSSY

This mode permits conversions that do not require truncation or other  special handling of the source value; that is, it permits conversions where  the target type has a wider range than the source type. 【確保從庫的列類型更寬泛些也不會導致復制報錯】

Setting this  mode has no bearing on whether lossy conversions are permitted; this is  controlled with the ALL_LOSSY mode. If  only ALL_NON_LOSSY is set,  but not ALL_LOSSY, then  attempting a conversion that would result in the loss of data (such as INT to TINYINT, or CHAR(25) to VARCHAR(20)) causes the slave to stop with an  error.

ALL_LOSSY,ALL_NON_LOSSY

When this mode is set, all supported type conversions are permitted, whether  or not they are lossy conversions.

ALL_SIGNED

Treat promoted integer types as signed values (the default behavior).

ALL_UNSIGNED

Treat promoted integer types as unsigned values.

ALL_SIGNED,ALL_UNSIGNED

Treat promoted integer types as signed if possible, otherwise as  unsigned.

[empty]

When slave_type_conversions is not  set, no attribute promotion or demotion is permitted; this means that all  columns in the source and target tables must be of the same types.

This mode is the default.


生產(chǎn)環(huán)境一個案例:

從庫是有個oracle系統(tǒng)去查數(shù)據(jù)的,原先從庫的cp_shop_activity表示utf8mb4字符集,oracle不支持。因此在這個從庫上手動修改了mode字段的字符類型為varchar(200) utf8,修改后的字段類型如下圖:

MySQL從庫的列類型不一致導致的復制異常問題


但是沒幾天后,發(fā)現(xiàn)主從復制報錯了,錯誤碼1677。從庫上記錄的錯誤日志如下:

MySQL從庫的列類型不一致導致的復制異常問題


我們看下主庫的mode字段是varchar(50),從庫的mode字段是varchar(200)。

MySQL從庫的列類型不一致導致的復制異常問題


在從庫設(shè)置:

stop slave;

set global slave_type_conversions=ALL_NON_LOSSY;  

# 默認slave_type_conversions為空,表示強制從庫和主庫的字段類型一致,不然就停止復制。設(shè)置為ALL_NON_LOSSY就稍微寬泛些同時確保不會造成類型轉(zhuǎn)換的問題。

start slave;

show slave status\G  

這樣即可解決這個問題。

 


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI