首页 > 数据库 >GreatSQL一个关于主从复制的限制描述与规避

GreatSQL一个关于主从复制的限制描述与规避

时间:2023-09-27 09:57:36浏览次数:43  
标签:主从复制 DEFAULT xxx GreatSQL greatsql 规避 NUM Master NULL

一、背景

分享一个在项目运维中遇到的一个主从复制限制的一个坑,项目的架构为主集群+灾备集群,每个集群为一主两从模式。主集群到灾备集群的同步为主从复制的方式,根据业务需求灾备集群需要忽略系统库跟某些配置表,所以才会触发此限制,而这个限制如果我们之前没有遇到过,那么排查起来也是相对不易的。

二、限制描述

1、主从同步出现报错

greatsql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.xxx.xxx
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: greatsql-bin.000990
          Read_Master_Log_Pos: 92274290
               Relay_Log_File: greatsql-relay.002963     -----
                Relay_Log_Pos: 701548899
        Relay_Master_Log_File: greatsql-bin.000988
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB: mysql,dbscale,dbscale_tmp,information_schema,performance_schema,sys
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table: A.ab,B.bc
                   Last_Errno: 1146
                   Last_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction '9e668a93-2618-11ee-93ee-bc16954181bb:47508257' at master log greatdb-bin.000988, end_log_pos 701570116. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 701548690
              Relay_Log_Space: 2246320360
              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: 1146
               Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction '9e668a93-2618-11ee-93ee-bc16954181bb:47508257' at master log greatdb-bin.000988, end_log_pos 701570116. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1943306
                  Master_UUID: 9e668a93-2618-11ee-93ee-bc16954181bb
             Master_Info_File: mysql.slave_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: 230822 14:14:18
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 9e668a93-2618-11ee-93ee-bc16954181bb:2-47565802
            Executed_Gtid_Set: 30873cfe-8750-11ed-b56f-744aa4073024:1-270,
9e668a93-2618-11ee-93ee-bc16954181bb:1-47508256
                Auto_Position: 1
         Replicate_Rewrite_DB:  
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

根据slave status状态信息可以看出

  • 报错的GTID为:'9e668a93-2618-11ee-93ee-bc16954181bb:47508257'
  • 应用的主集群的binlog为:greatsql-bin.000988
  • 灾备集群的relay log为:greatsql-relay.002963

详细信息查看performance_schema.replication_applier_status_by_worker

2、查看错误的详细信息

greatsql> select * from performance_schema.replication_applier_status_by_worker\G
*************************** 1. row ***************************
         CHANNEL_NAME:
            WORKER_ID: 1
            THREAD_ID: NULL
        SERVICE_STATE: OFF
LAST_SEEN_TRANSACTION: 9e668a93-2618-11ee-93ee-bc16954181bb:47508257
    LAST_ERROR_NUMBER: 1146
   LAST_ERROR_MESSAGE: Worker 1 failed executing transaction
'9e668a93-2618-11ee-93ee-bc16954181bb:47508257' at master log greatdb-bin.000988,
end_log_pos 701570116; Error executing row event: 'Table 'abs_xxx.tmp_xxx_info' doesn't exist'
LAST_ERROR_TIMESTAMP: 2023-08-22 14:14:18

上述信息说明根据performance_schema.replication_applier_status_by_worker表中的详细错误信息可以发现为灾备集群abs_xxx.tmp_xxx_info表不存在,导致同步报错

3、问题分析

3.1、确认灾备集群中目标表是否存在

greatsql> show create table abs_xxx.tmp_xxx_info;
ERROR 1146 (42S02): Table 'abs_xxx.tmp_xxx_info' doesn't exist
greatsql> desc abs_xxx.tmp_xxx_info;
ERROR 1146 (42S02): Table 'abs_xxx.tmp_xxx_info' doesn't exist

结论:灾备集群中目标表的确不存在

3.2、根据主从报错信息解析主集群binlog,报错的SQL

解析主集群binlog

SET @@SESSION.GTID_NEXT= '9e668a93-2618-11ee-93ee-bc16954181bb:47508257'/*!*/;
……
#230822 14:14:18 server id 1943306  end_log_pos 701570000         Table_map: `abs_xxx`.`tmp_xxx_info` mapped to number 1595
# at 701570000
#230822 14:14:18 server id 1943306  end_log_pos 701570116         Write_rows: table id 1595 flags: STMT_END_F
### INSERT INTO `abs_xxx`.`tmp_xxx_info`
### SET
###   @1=2
###   @2='自动化'
###   @3='2300121212120000'
###   @4='90000000'
###   @5='1'
###   @6='202001290231001'
###   @7='2021-01-31 00:00:00'
# at 701570116
#230822 14:14:18 server id 1943306  end_log_pos 701570143         Xid = 800998400
COMMIT/*!*/;
# at 701570143
#230822 14:14:18 server id 1943306  end_log_pos 701570204         GTID        last_committed=26491        sequence_number=26521        rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;

结论:根据复制的报错信息得知具体的GTID号以及主集群的binlog文件,解析binlog得知此事务为一条INSERT语句,语句中的目标表与performance_schema.replication_applier_status_by_worker表中信息一致

3.3、寻找主集群目标表binlog中是否有建表语句

在同一binlog日志中寻找建表语句

SET TIMESTAMP=1692684495/*!*/;
CREATE DATABASE IF NOT EXISTS `abs_xxx` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */
/*!*/;
……
use `information_schema`/*!*/;
SET TIMESTAMP=1692684495/*!*/;
CREATE TABLE `abs_xxx`.`tmp_xxx_info` (
  `ID` int(64) NOT NULL AUTO_INCREMENT,
  `CUSTOMER_NAME` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `CUSTOMER_NUM` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `PRODIST_SKU_NUM` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
  `STATUS` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
  `AGREEMENT_NUM` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `END_DATE` datetime DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `PK_PRODIST_SKU_NUM_AGREEMENT` (`PRODIST_SKU_NUM`) USING BTREE,
  KEY `IDX_AGREEMENT_NUM` (`AGREEMENT_NUM`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC
/*!*/;
# at 475864451

结论:在主集群的binlog日志中找到了目标表的建表语句,说明主集群执行DDL时并没有关闭binlog日志,那么继续查看在灾备集群的中继日志中是否存在DDL语句

3.4、解析灾备集群的中继日志,确认是否拉取到灾备集群

#230822 14:08:15 server id 1943306  end_log_pos 475863662         GTID        last_committed=16341        sequence_number=16342        rbr_only=no
SET @@SESSION.GTID_NEXT= '9e668a93-2618-11ee-93ee-bc16954181bb:47498079'/*!*/;
……
use `information_schema`/*!*/;
SET TIMESTAMP=1692684495/*!*/;
CREATE TABLE `abs_xxx`.`tmp_xxx_info` (
  `ID` int(64) NOT NULL AUTO_INCREMENT,
  `CUSTOMER_NAME` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `CUSTOMER_NUM` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `PRODIST_SKU_NUM` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
  `STATUS` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
  `AGREEMENT_NUM` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `END_DATE` datetime DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `PK_PRODIST_SKU_NUM_AGREEMENT` (`PRODIST_SKU_NUM`) USING BTREE,
  KEY `IDX_AGREEMENT_NUM` (`AGREEMENT_NUM`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC
/*!*/;
# at 475864660
#230822 14:08:15 server id 1943306  end_log_pos 475864512         GTID        last_committed=16342        sequence_number=16343        rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; 

结论:灾备集群的中继日志中存在DDL建表语句,说明并不是IO线程出了问题

3.5、排查复制配置的忽略库表

Replicate_Ignore_DB: mysql,dbscale,dbscale_tmp,information_schema,performance_schema,sys
Replicate_Wild_Ignore_Table: A.ab,B.bc

结论:忽略库表中并不包含目标表,但是根据以上解析日志发现,在主集群binlog日志中建表语句之前有个use information_schema/!/; 的语句,此库为同步忽略的系统库,因此触发了GreatSQL的规范限制,在忽略库下对未忽略进行操作Statement模式下记录语句默认不起作用 (详情:https://dev.mysql.com/doc/refman/5.7/en/replication-options-replica.html#option_mysqld_replicate-do-db)

4、解决同步报错

在灾备集群创建目标表

greatsql> CREATE TABLE `abs_xxx`.`tmp_xxx_info` (
  `ID` int(64) NOT NULL AUTO_INCREMENT,
  `CUSTOMER_NAME` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `CUSTOMER_NUM` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `PRODIST_SKU_NUM` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
  `STATUS` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL,
  `AGREEMENT_NUM` varchar(256) COLLATE utf8mb4_bin DEFAULT NULL,
  `END_DATE` datetime DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `PK_PRODIST_SKU_NUM_AGREEMENT` (`PRODIST_SKU_NUM`) USING BTREE,
  KEY `IDX_AGREEMENT_NUM` (`AGREEMENT_NUM`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;

greatsql> stop slave;
greatsql> start slave;

结论:在灾备集群创建目标表后重启复制恢复成功

三、限制规避

1、第一种规避方式

执行DDL时进入目标库

greatsql> use abs_cust
greatsql> DDL 语句(CREATE\DROP\ALTER)

说明:在应用连接数据库时有可能默认就是information_schema库,而此环境将系统库全部忽略,所以为了规避类似的问题,请在执行SQL语句时请先use到目标表的目标库。

2、第二种规避方式

修改主从复制配置,以下步骤为测试环境

关闭灾备集群在复制同步

greatsql> stop slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)

修改忽略库

greatsql> change replication filter Replicate_Ignore_DB=();

修改忽略表

greatsql> change replication filter replicate_wild_ignore_table =('mysql.%','information_schema.%','sys.%','performance_schema.%');

启动同步

greatsql> start slave;
Query OK, 0 rows affected, 1 warning (0.37 sec)

测试验证

主集群:

greatsql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

greatsql> create table test111.test111(id int primary key);
Query OK, 0 rows affected (0.06 sec)

greatsql> show tables;
+-------------------+
| Tables_in_test111 |
+-------------------+
| test111           |
+-------------------+
1 row in set (0.00 sec)

灾备集群:

greatsql> use test111
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

greatsql> show tables;
+-------------------+
| Tables_in_test111 |
+-------------------+
| test111           |
+-------------------+
1 row in set (0.00 sec)

说明:复制配置中参数Replicate_Ignore_DB设置为空,将replicate_wild_ignore_table参数设置为shema_name.%的方式也可以规避类似的问题

四、特别说明

  • 在MySQL 5.7跟8.0版本也存在此限制

Enjoy GreatSQL

标签:主从复制,DEFAULT,xxx,GreatSQL,greatsql,规避,NUM,Master,NULL
From: https://www.cnblogs.com/greatsql/p/17731957.html

相关文章

  • Redis 主从复制、哨兵模式、群集模式
    Redis主从复制、哨兵模式、群集模式目录1.Redis的2种持久化方式2.Redis优化3.Redis主从复制的原理3.1配置文件3.2Redis主从复制4.Redis哨兵模式4.1哨兵机制4.2哨兵模式的作用4.3哨兵结构4.4哨兵模式配置1.Redis的2种持久化方式RDB持久化定时把red......
  • MySQL主从复制实践
    MySQL主从复制实践多个数据库间主从复制与读写分离的模型​ 在一主多从的数据库体系中,多个从服务器采用异步的方式更新主数据库的变化;业务服务器在执行写或者相关修改数据库的操作是在主服务器上进行的,读操作则是在各从服务器上进行​ Mysql主从复制的实现原理图大致如下​......
  • MySQL主从复制与读写分离
    一、MySQL主从复制与读写分离1、什么是读写分离?读写分离,基本的原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。2、为什么要读写分离呢?因为数据库的“写”(写......
  • Redis主从复制,高可用性面试题
    参考链接:https://xiaolincoding.com/redis/cluster/master_slave_replication.html#%E7%AC%AC%E4%B8%80%E6%AC%A1%E5%90%8C%E6%AD%A5 主从第一步同步的过程? 分成三步进行:1、建立连接,从服务器获得主服务的id和复制位置,一开始是-1。2、主服务器fork一个子进程用来创建当前的R......
  • MySQL 主从复制与读写分离
    MySQL主从复制与读写分离1、什么是读写分离?读写分离,基本的原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。2、为什么要读写分离呢?因为数据库的“写”(写10000......
  • postgresql主从复制状态检查
    确定主从库方法一ps-ef|grep"wal"|grep-v"grep如果输出walsender…streaming进程说明当前数据库为主库如果输出walreceiverstreaing进程说明当前数据库为备用库方法二select*frompg_stat_replication;在主库上查询pg_stat_replication视图,如果返回记录说明是主库,备库上......
  • MySQL主从复制原理
    一张图让你牢记MySQL主从复制原理|原创(qq.com)为什么需要主从复制?1、读写分离,增强MySQL数据库的可用性。2、做数据的热备。3、架构的扩展。业务量越来越大,I/O访问频率过高,单机无法满足,此时做多库的存储,降低磁盘I/O访问的频率,提高单个机器的I/O性能。说说BinlogMySQL的Server......
  • 极验率先推出一键认证安全版,供客户自主免费升级,规避日常运营中的风险盲区
    2017年6月1日,互联网服务开始响应《中华人民共和国网络安全法》的要求实施账号实名认证。由此,手机号码成为网络世界最主要的“身份证”,也让本机号码一键认证成为可能。其中,极验是最早的直连三大运营商的五家供应商之一,早在2017年就正式对外提供一键认证接口。一键认证于2020年进入普......
  • redis主从复制
    相关配置a.replica-read-onlyyes:从节点开启只读模式b.master-authxxxx:主节点访问密码c.replicaofip端口:从哪个主节点进行复制相关命令inforeplication查看主从信息主从复制分类主从刚连接的时候,会进行全量同步;全同步后,会进行增量同步。a.全量复制 i.mast......
  • Mysql主从复制原理
     本文总结自网上关于Mysql复制原理的一些文章。----------------------------------------------------------1、Mysql复制是一个异步的复制,从masterinstance到slaveinstance,实现整个复制操作主要使用3个进程完成。其中2个进程是Slave的Sql进程和IO进......