【MySql】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)

时间: 2024-08-01 18:28:49

【MySql】sql_slave_skip_counter 参数的用法解析的相关文章

对MySQL配置参数 my.ini/my.cnf的详细解析_Mysql

以下的文章主要描述的是对MySQL配置参数 my.ini/my.cnf的详细解析,我们主要是以实例演示的方式来对MySQL配置参数 my.ini/my.cnf的实际操作步骤进行说明,以下就是相关内容的具体描述. 1.获取当前配置参数 要优化MySQL配置参数,首先要了解当前的配置参数以及运行情况.使用下列命令可以获得目前服务器使用的配置参数: 复制代码 代码如下: mysqld –verbose –help mysqladmin variables extended-status –u root

Mysql中limit的用法详解

Mysql中limit的用法:在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数.LIMIT 接受一个或两个数字参数.参数必须是一个整数常量.如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最

mysql limit的分页用法与性能优化

mysql教程 limit 的性能问题 有个几千万条记录的表 on mysql 5.0.x,现在要读出其中几十万万条左右的记录 常用方法,依次循环: select * from mytable where index_col = xxx limit offset, limit; 经验:如果没有blob/text字段,单行记录比较小,可以把 limit 设大点,会加快速度 问题:头几万条读取很快,但是速度呈线性下降,同时 mysql server cpu 99% 速度不可接受. 调用 explai

深入分析Mysql中limit的用法_Mysql

Mysql中limit的用法:在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT * FROM table   LIMIT [offset,] rows | rows OFFSET offset LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数.LIMIT 接受一个或两个数字参数.参数必须是一个整数常量.如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行

MySQL中show命令用法大全

MySQL中show命令用法大全 官方文档:https://dev.mysql.com/doc/refman/5.6/en/show.htmlhttps://dev.mysql.com/doc/refman/5.7/en/extended-show.html 13.7.5.1 SHOW AUTHORS Syntax 13.7.5.2 SHOW BINARY LOGS Syntax 13.7.5.3 SHOW BINLOG EVENTS Syntax 13.7.5.4 SHOW CHARACTER

MySQL配置文件之mysql.ini参数详解

  my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数. my.ini分为两块:Client Section和Server Section. Client Section用来配置MySQL客户端参数. 要查看配置参数可以用下面的命令: show variables like '%innodb%'; # 查看innodb相关配置参数 show status like '%innodb%'; # 查看innodb相关的运行时参数(比如当前

mysql数据库KEY分区用法

  mysql数据库KEY分区用法 按照KEY进行分区类似于按照HASH分区,除了HASH分区使用的用户定义的表达式,而KEY分区的 哈希函数是由MySQL 服务器提供.MySQL 簇(Cluster)使用函数MD5()来实现KEY分区;对于使用其他存储引擎的表,服务器使用其自己内部的 哈希函数,这些函数是基于与PASSWORD()一样的运算法则. "CREATE TABLE ... PARTITION BY KEY"的语法规则类似于创建一个通过HASH分区的表的规则.它们唯一的区别在

Jquery中ajax方法data参数的用法小结

 本篇文章主要是对Jquery中ajax方法data参数的用法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助  代码如下: $.ajax({    type: "POST",    url: "some.php",    data: "name=John&location=Boston", //第一种方式传参   // data: {name:"John",location:"Boston&qu

linux驱动-各位大神,谁能跟我讲讲file_Operation结构里面各个函数的具体用法,特别是函数参数的用法,感激不尽

问题描述 各位大神,谁能跟我讲讲file_Operation结构里面各个函数的具体用法,特别是函数参数的用法,感激不尽 各位大神,谁能跟我讲讲file_Operation结构里面各个函数的具体用法,特别是函数参数的用法,感激不尽 解决方案 file_operation是Linux内核驱动中一个非常重要的结构体,它建立了用户空间与内核空间之间的联系.file_operation结构体定义在linux内核的includelinuxfs.h文件中,定义是这样的: ```struct file_oper