mysql运维-slave_skip_errors

1 简介
    mysql在主从复制过程中,由于各种的原因,从服务器可能会遇到执行BINLOG中的SQL出错的情况,在默认情况下,服务器会停止复制进程,不再进行同步,等到用户自行来处理。
    slave-skip-errors的作用就是用来定义复制过程中从服务器可以自动跳过的错误号,当复制过程中遇到定义的错误号,就可以自动跳过,直接执行后面的SQL语句。
2 官方参考

Command-Line Format

--slave-skip-errors=name

System Variable Name

slave_skip_errors

Variable Scope

Global

Dynamic Variable

No

 

Permitted Values

Type

string

Default

OFF

Valid Values

OFF

[list of error codes]

all

ddl_exist_errors

    slave_skip_errors选项有四个可用值,分别为: off,all,ErorCode,ddl_exist_errors
     默认情况下该参数值是off,我们可以列出具体的error code,也可以选择all,mysql5.6及MySQL Cluster NDB 7.3以及后续版本增加了参数ddl_exist_errors,该参数包含一系列error code(1007,1008,1050,1051,1054,1060,1061,1068,1094,1146)
    一些error code代表的错误如下:
     1007: 数据库已存在,创建数据库失败
     1008: 数据库不存在,删除数据库失败
     1050: 数据表已存在,创建数据表失败
     1051: 数据表不存在,删除数据表失败
     1054: 字段不存在,或程序文件跟数据库有冲突
     1060: 字段重复,导致无法插入
     1061: 重复键名
     1068: 定义了多个主键
     1094: 位置线程ID
    1146: 数据表缺失,请恢复数据库
    1053: 复制过程中主服务器宕机
     1062: 主键冲突 Duplicate entry '%s' for key %d

    my.cnf中的写法:
[sql]  view plain  copy
  1. slave_skip_errors=1062,1053  
  2. slave_skip_errors=all  
  3. slave_skip_errors=ddl_exist_errors  
    作为mysql启动参数的写法:
[sql]  view plain  copy
  1. --slave-skip-errors=1062,1053  
  2. --slave-skip-errors=all  
  3. --slave-skip-errors=ddl_exist_errors  

    从数据库中查看该参数的值:

[sql]  view plain  copy
  1. mysql> show variables like 'slave_skip%';  
  2. +-------------------+-------+  
  3. | Variable_name     | Value |  
  4. +-------------------+-------+  
  5. | slave_skip_errors | 1007  |  
  6. +-------------------+-------+  
3 举例分析

    3.1 测试说明
    配置好mysql主从同步,然后在从上写入数据,造成主从不一致。
    3.2 准备测试表结构
    在主机上创建表:

[sql]  view plain  copy
  1. create table replication (c1 int not null primary key, c2 varchar(10));  
    3.3 准备测试数据
    在主机上插入基础数据
[sql]  view plain  copy
  1. mysql> insert into replication values (1, 'test1');  
  2. mysql> insert into replication values (2, 'test2');  

    此时,主机从机replication表里面都有两条记录
    3.4 开始测试
    从机插入一条记录

[sql]  view plain  copy
  1. mysql> insert into replication values (3, 'test3');  
    然后在主机上执行相同的操作

[sql]  view plain  copy
  1. mysql> insert into replication values (3, 'test3');  
    在从机上查看复制状态

[sql]  view plain  copy
  1. mysql> show slave status \G  
  2. *************************** 1. row ***************************  
  3.                Slave_IO_State: Waiting for master to send event  
  4.                   Master_Host: 192.168.1.222  
  5.                   Master_User: repl  
  6.                   Master_Port: 3306  
  7.                 Connect_Retry: 60  
  8.               Master_Log_File: mysql-bin.000003  
  9.           Read_Master_Log_Pos: 16700  
  10.                Relay_Log_File: mysql-relay-bin.000003  
  11.                 Relay_Log_Pos: 16595  
  12.         Relay_Master_Log_File: mysql-bin.000003  
  13.              Slave_IO_Running: Yes  
  14.             Slave_SQL_Running: No  
  15.               Replicate_Do_DB:   
  16.           Replicate_Ignore_DB:   
  17.            Replicate_Do_Table:   
  18.        Replicate_Ignore_Table: mysql.ibbackup_binlog_marker  
  19.       Replicate_Wild_Do_Table:   
  20.   Replicate_Wild_Ignore_Table: mysql.backup_%  
  21.                    Last_Errno: 1062  
  22.                    Last_Error: Error 'Duplicate entry '3' for key 'PRIMARY'' on query. Default database'test'. Query: 'insert into replication values (3, 'test3')'  
  23.                  Skip_Counter: 0  
  24.           Exec_Master_Log_Pos: 16425  
  25.               Relay_Log_Space: 17544  
    可以看到:sql线程已经停止工作 Slave_SQL_Running: No
                        错误号为:Last_Errno: 1062
                        错误信息为:Last_Error: Error 'Duplicate entry '3' for key 'PRIMARY'' on query. Default database: 'test'. Query: 'insert into replication values (3, 'test3')'
    如果我们在my.cnf中加入如下选项,则可跳过此错误,数据同步继续进行。

[sql]  view plain  copy
  1. [mysqld]  
  2. slave_skip_errors=1062  
    具体测试方法同上,大家可自己验证。

4 从backup恢复时从机复制出错的一些解释

    mysql企业版备份工具meb提供在线热备功能,如果在备份过程中执行ddl操作,从机需要从主机的备份恢复时可能会异常,从而导致从机同步数据失败。原因是从机恢复时需要先从备份文件恢复(包含备份过程中执行的ddl语句),同步时不是从全备后的最后一个位置同步,而是从ddl的上个位置同步,如果再次执行该ddl语句在从机上不会造成冲突,则同步继续,如果会造成冲突,同步终止。解决此冲突的办法是在my.cnf文件中加入一行

[sql]  view plain  copy
  1. [mysqld]  
  2. slave_skip_errors=ddl_exist_errors  
5 注意事项
    5.1 该参数为全局静态参数,不能动态调整,可在my.cnf中加入该参数列表后重启mysql服务器生效。
    5.2 必须注意的是,启动这个参数,如果处理不当,很可能造成主从数据库的数据不同步,在应用中需要根据实际情况,如果对数据完整性要求不是很严格,那么这个选项确实可以减轻维护的成本

****************************************************************************************

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Master configuration Find the my.cnf file Configure under [mysqld] [mysqld] server-id=1 log-bin=master-bin   Restart the service and log in to mysql Create a user to obtain the log file. mysql> CREATE USER 'sree'@'%' IDENTIFIED BY 'sree' Grant relevant permissions (copy permissions) *.* represents all tables in all libraries mysql> grant replication slave on *.* to 'sree'@'%'   Refresh permissions mysql> flush privileges mysql> show master status; | File | Position | master-bin.000004 | 120                      Slave configuration ==================== Modify the configuration file: Configure under [mysqld] [mysqld] server-id=2 #As long as it is different from the above.   Then log in to mysql mysql>  CHANGE MASTER TO     MASTER_LOG_FILE='master-bin.000004 ', #above file        MASTER_LOG_POS = 120 ; #The above position        MASTER_HOST='192.168.249.130', #The ip address of the main library         MASTER_USER = 'sree',         MASTER_PASSWORD = 'sree',  Then start slave ============ mysql> show slave status\G; see: ===  Slave_IO_Running: Yes  Slave_SQL_Running: Yes It means that it was successful.     Errors in the whole process: Slave_IO_Running: Connecting 1. Mine is the wrong host ip. 2. There are still some firewalls on the Internet that are not turned off I use centos7 to turn off the firewall: systemctl stop firewalld 3. There is also a mistake in the above-mentioned secret.   Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs This error generally occur when we clone the master to slaver. Delete auto.cnf of mysql, and then restart the service.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值