【MySql】sql_slave_skip_counter 参数的用法解析

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:    前一篇文章介绍了当mysql的复制出现slave sql 进程终止时的解决办法,其中之一使用了sql_slave_skip_counter 来使sql 线程跳过遇到错误的事件记录!本文浅析一下sql_slave_skip_counter的具体...
   前一篇文章介绍了当mysql的复制出现slave sql 进程终止时的解决办法,其中之一使用了 sql_slave_skip_counter 来使sql 线程跳过遇到错误的事件记录!本文浅析一下 sql_slave_skip_counter的具体用法和意义!
set global sql_slave_skip_counter = N
This statement skips the next N events from the master. 
(即是跳过N个events,这里最重要的是理解event的含义!在mysql中,对于sql的 binary log 实际上是由一连串的event组成的一个组,即事务组。)
在备库上设置 global sql_slave_skip_counter =N 会跳过当前时间来自于master的之后N个事件,这对于恢复由某条SQL语句引起的从库复制有效. 此语句只在当slave threads是停止时才有效,否则将发生一条错误..每忽略一个事件,N 减一,直到N减为0!
When using this statement, it is important to understand that the binary log is actually organized as a sequence of groups known as event groups. Each event group consists of a sequence of events.
For transactional tables, an event group corresponds to a transaction.
For nontransactional tables, an event group corresponds to a single SQL statement.
Note
   A single transaction can contain changes to both transactional and nontransactional tables.When you use SET GLOBAL sql_slave_skip_counter to skip events and the result is in the middle of a group, the slave continues to skip events until it reaches the end of the group. Execution then starts with the next event group
### comment ###
   Setting this variable isn't like setting other server variables: you can't read the variable back again as @@sql_slave_skip_counter, and it isn't really a "global variable." Rather, it's a variable that only the slave thread reads.
  When you restart the slave threads again with START SLAVE, the slave skips statements and decrements the variable until it reaches 0, at which point it begins executing statements again. You can watch this happening by executing SHOW SLAVE STATUS, where the variable's value appears in the Skip_Counter column. This is the only place you can see its value.
  The effect is that the setting isn't persistent. If you set it to 1, start the slave, and the slave has an error in replication sometime later, the variable won't still be set to 1. It'll be 0. At that point, if you want the slave to skip the statement that caused the error, you'll have to set it to 1 again.
有关"SET GLOBAL sql_slave_skip_counter"的语法可以参考官方文档
测试环境:
rac3 主库
rac4 备库
测试之前保证主从无数据延时,保证数据一致!
1 使用含有 stop slave 的命令, 在主库上创建测试表,并使用shell 插入数据!
mysql> create table tab_skip(id int);
Query OK, 0 rows affected (0.80 sec)
[root@rac3 mysql]#
 for i in {1..100}; 
   do 
    echo $i; 
    echo "insert into tab_skip(id) values($i)" | mysql -h127.0.0.1 test ; 
    sleep 1;
done;
在备库 使用 set global sql_slave_skip_counter=1;命令做测试
[root@rac4 mysql]# 
for i in {1..10}; 
do 
   echo $i; 
   echo "slave stop;set global sql_slave_skip_counter=1; slave start;show slave status\G" | mysql -h127.0.0.1 -P3306 test ; 
   sleep 2;
done;
分别在主库和备库上进行验证数据的完整性:
主库上面:
[root@rac3 mysql]# mysql
mysql> use test;                 
Database changed
mysql> select count(1) from tab_1;
+----------+
| count(1) |
+----------+
|      100 |
+----------+
1 row in set (0.00 sec)
备库上面,少了 10条数据!因为正是执行set global sql_slave_skip_counter=1;使备库执行sql replay的时候忽略了事件!
[root@rac4 mysql]# mysql
mysql> use test;
Database changed
mysql> select count(1) from tab_1;  
+----------+
| count(1) |
+----------+
|       90 |
+----------+
1 row in set (0.00 sec)

有网友测试的是在备库上执行没有stop slave 语句的命令,但是在5.5.18版本上面是不允许的!
[root@rac3 mysql]# for i in {1..100}; do echo $i; echo "insert into tab_2(id) values($i)" | mysql -h127.0.0.1 test ; sleep 2;done;   
1
....
100

在备库上执行,注:"set global sql_slave_skip_counter=1; slave start;show slave status\G"  没有stop slave 语句,报错!
[root@rac4 mysql]# for i in {1..10}; do echo $i; echo "set global sql_slave_skip_counter=1; slave start;show slave status\G" | mysql -h127.0.0.1 -P3306 test ; sleep 2;done;          
1
ERROR 1198 (HY000) at line 1: This operation cannot be performed with a running slave; run STOP SLAVE first
2
ERROR 1198 (HY000) at line 1: This operation cannot be performed with a running slave; run STOP SLAVE first
3
ERROR 1198 (HY000) at line 1: This operation cannot be performed with a running slave; run STOP SLAVE first
4
ERROR 1198 (HY000) at line 1: This operation cannot be performed with a running slave; run STOP SLAVE first

使用 该参数能够解决从服务器sql 进程停止导致的数据库不同步,但是也有一定的风险,比如在高并发的数据库环境下,可能会导致数据丢失!
另见另一位网友的 测试实验 (多少有些出入,他的可以不使用stop slave)
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
分布式计算 大数据 数据库连接
maxcompute开发环境问题之参数解析失败如何解决
MaxCompute开发环境是指设置和配置用于开发MaxCompute项目的本地或云端环境;本合集将指导用户如何搭建和管理MaxCompute开发环境,包括工具安装、配置和调试技巧。
|
1月前
|
机器学习/深度学习 算法 编译器
【C++ 泛型编程 中级篇】深度解析C++:类型模板参数与非类型模板参数
【C++ 泛型编程 中级篇】深度解析C++:类型模板参数与非类型模板参数
47 0
|
2月前
|
SQL 关系型数据库 MySQL
MySQL - 一文解析 SQL 的执行顺序
MySQL - 一文解析 SQL 的执行顺序
|
3天前
|
SQL 存储 关系型数据库
数据库开发之mysql前言以及详细解析
数据库开发之mysql前言以及详细解析
14 0
|
2月前
|
SQL 存储 关系型数据库
解析MySQL Binlog:从零开始的入门指南【binlog入门指南】
解析MySQL Binlog:从零开始的入门指南【binlog入门指南】
1126 0
|
1月前
|
缓存 前端开发 Java
【二十八】springboot之通过threadLocal+参数解析器实现同session一样保存当前登录信息的功能
【二十八】springboot之通过threadLocal+参数解析器实现同session一样保存当前登录信息的功能
34 1
|
14天前
|
存储 关系型数据库 MySQL
MySQL引擎对决:深入解析MyISAM和InnoDB的区别
MySQL引擎对决:深入解析MyISAM和InnoDB的区别
29 0
|
2月前
|
存储 SQL 关系型数据库
MySQL - 深入解析MySQL索引数据结构
MySQL - 深入解析MySQL索引数据结构
|
3天前
|
前端开发 Java
SpringBoot之实体参数的详细解析
SpringBoot之实体参数的详细解析
10 0
|
27天前
|
canal 消息中间件 关系型数据库
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
76 0

推荐镜像

更多