MySQL从库的列类型不一致导致的复制异常问题
admin
2023-05-20 07:02:51
0

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


slave_type_conversions  这个参数在mysql5.5.3 引入,目的是启用row 格式的bin-log 的时候,如果主从的column 的数据类型不一致,会导致复制失败,mysql5.5.3 之后支持,主库是int 从库是bigint 这种类型的复制,

这个参数的意义就是控制些类型转换容错性。

 

如果从库的字段类型范围比主库类型大,那么设置slave_type_conversions=ALL_NON_LOSSY后复制没有问题的。

如果从库类型比主库类型小,比如从int 复制到tinyint ,虽然可以通过设置slave_type_conversions=ALL_LOSSY,ALL_NON_LOSSY让主从不出问题,但是实际上会造成数据丢失的风险。

 

几种值的设置:

    ALL_LOSSY允许数据截断

    ALL_NON_LOSSY不允许数据截断,如果从库类型大于主库类型,是可以复制的,反过了,就不行了,从库报复制错误,复制终止。

    ALL_LOSSY,ALL_NON_LOSSY: 所有允许的转换都会执行,而不管是不是数据丢失。

    空值(不设置)要求主从库的数据类型必须严格一致,否则都报错。

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.


生产环境一个案例:

从库是有个oracle系统去查数据的,原先从库的cp_shop_activity表示utf8mb4字符集,oracle不支持。因此在这个从库上手动修改了mode字段的字符类型为varchar(200) utf8,修改后的字段类型如下图:

MySQL从库的列类型不一致导致的复制异常问题


但是没几天后,发现主从复制报错了,错误码1677。从库上记录的错误日志如下:

MySQL从库的列类型不一致导致的复制异常问题


我们看下主库的mode字段是varchar(50),从库的mode字段是varchar(200)

MySQL从库的列类型不一致导致的复制异常问题


在从库设置:

stop slave;

set global slave_type_conversions=ALL_NON_LOSSY;  

# 默认slave_type_conversions为空,表示强制从库和主库的字段类型一致,不然就停止复制。设置为ALL_NON_LOSSY就稍微宽泛些同时确保不会造成类型转换的问题。

start slave;

show slave status\G  

这样即可解决这个问题。

 


相关内容

热门资讯

他扮演蜘蛛侠涨粉千万,流量密码... Joshua Watson(约书亚·沃森)是一位美国知名的视觉特效(VFX)内容创作者和社交媒体影响...
国企信创数字化改造,部署 AI... 国企信创数字化改造,最难的不是把 AI 智能体“装上去”,而是让它在国产电脑、国产操作系统和现有业务...
三宜电气申请户外高压限流熔断器... 国家知识产权局信息显示,浙江三宜电气有限公司申请一项名为“一种户外高压限流熔断器”的专利,公开号CN...
AI+物理未来在哪?量子计算大... 8月3日,为期三天的“第五届CCF量子计算大会暨大湾区量子科学论坛”(以下简称“量子计算大会”)在深...
求问空气净化器有用吗?什么品牌... 最佳回答 空气净化器当然是有用的呀,真的可以起到净化室内空气的作用。目前市面上卖得最火的空气净化器都...
哪个品牌的座便器好 我家用的就是箭牌价格有贵的有便宜的,性价比比较高,他们的价格1000多的20000的都有像我们买20...
燃气热水器坏了能修吗 燃气热水器是我们日常生活中经常使用的家电之一,它可以帮我们快速制热热水,满足我们生活中的各种需求。但...
热水器温度计坏了能修吗 热水器是我们生活中必不可少的家用电器之一,尤其在冬季使用更加频繁。热水器温度计是热水器的核心部件之一...
管道接口处漏水怎么办 如果是管道接口处的接头松动了,可以用工具拧一下。如果是已经裂开了,要借助水泥、防水胶、防水剂之类的东...
外卖小哥一着急,给警察派了一个... 近日,北京街头上演了一场温暖的“爱心接力”。外卖小哥卢浩然情急之下拨打110报警,给民警派了个特殊的...