MySQL主从复制中若是出现错误,一般有哪些解决方法?通读本文,相信你会有答案。

主从复制中若是出现错误可以通过几个方法来进行解决:

1. 如果主从复制时发生了主键冲突,从而阻止了主从复制,可以使用sql_slave_skip_counter这个变量来忽略错误将其排除

2. 如果发生了较大的错误,可以考虑使用reset slave的方法重新配置从服务器来恢复错误

以下演示如何使用这两种方法解决错误,及相关操作的详细说明

  • reset slave的使用方法

  • 环境准备搭建主从同步

  • 主节点配置

1. 修改配置文件

[root@Master ~]# vim /etc/my.cnf
[mysqld]
log-bin=/data/bin/mysql-bin
binlog-format=row
server-id=1

2. 创建二进制日志目录

[root@Master ~]# mkdir /data/bin
[root@Master ~]# chown -R mysql.mysql /data/bin

3. 启动mysqld服务

[root@Master ~]# systemctl start mariadb

4. 查看主服务器日志位置

[root@Master ~]# mysql -e "SHOW MASTER LOGS;"
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |     26753 |
| mysql-bin.000002 |    921736 |
| mysql-bin.000003 |       245 |
+------------------+-----------+

5. 创建一个用来复制数据的账户

[root@Master ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.73.%' IDENTIFIED BY 'CentOS';"

从节点配置

1. 修改配置文件

[root@Slave ~]# vim /etc/my.cnf
[mysqld]
read-only
server-id=2

2. 启动服务

[root@Slave ~]# systemctl start mariadb

此处开始构建错误配置,以下所有CHANGE MASTER TO配置均为错误

3. 配置CHANGE MASTER TO

  MariaDB [(none)]> CHANGE MASTER TO ->   MASTER_HOST='master2.mycompany.com', ->   MASTER_USER='replication',   ->   MASTER_PASSWORD='bigs3cret', ->   MASTER_PORT=3306,  ->   MASTER_LOG_FILE='master2-bin.001',   ->   MASTER_LOG_POS=4,  ->   MASTER_CONNECT_RETRY=10;   Query OK, 0 rows affected (0.00 sec)

4. 查看下SLAVE STATUS

    MariaDB [(none)]> SHOW SLAVE STATUS\G; *************************** 1. row ***************************  Slave_IO_State:     Master_Host: master2.mycompany.com  Master_User: replication    Master_Port: 3306   Connect_Retry: 10   Master_Log_File: master2-bin.001    Read_Master_Log_Pos: 4  Relay_Log_File: mariadb-relay-bin.000001    Relay_Log_Pos: 4    Relay_Master_Log_File: master2-bin.001  Slave_IO_Running: No    Slave_SQL_Running: No   ...以下省略...

5. 启动复制线程

MariaDB [(none)]> START SLAVE;

6. 再次查看SLAVE STATUS

    MariaDB [(none)]> SHOW SLAVE STATUS\G; *************************** 1. row ***************************  Slave_IO_State: Connecting to master    Master_Host: master2.mycompany.com  Master_User: replication    Master_Port: 3306   Connect_Retry: 10   Master_Log_File: master2-bin.001    Read_Master_Log_Pos: 4  Relay_Log_File: mariadb-relay-bin.000001    Relay_Log_Pos: 4    Relay_Master_Log_File: master2-bin.001  Slave_IO_Running: Connecting    Slave_SQL_Running: Yes  ...以下省略...

线程已经正常启动

主服务器导入数据进行测试

 [root@Master ~]# mysql < hellodb_innodb.sql     [root@Master ~]# mysql -e "SHOW DATABASES;"  +--------------------+    | Database           |  +--------------------+    | information_schema |  | hellodb            |  | mysql              |  | performance_schema |  | test               |  +--------------------+

从服务器查看是否同步(CHANGE MASTER TO信息不对怎么可能同步)

    MariaDB [(none)]> SHOW DATABASES;    +--------------------+    | Database           |  +--------------------+    | information_schema |  | mysql              |  | performance_schema |  | test               |  +--------------------+    4 rows in set (0.00 sec)

以下为错误解决方法

由于错误发生在CHANGE MASTER TO所以此处将CHANG MASTER TO部分纠正就行

1. 首先将从服务器的复制线程停止

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (17.48 sec)

2. 将从服务器上的SLAVE信息重置

MariaDB [(none)]> RESET SLAVE;
Query OK, 0 rows affected (0.01 sec)

3. 重新输入正确的CHANGE MASTER TO信息

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.73.110',MASTER_USER='repluser',MASTER_PASSWORD='centos',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)

4. 查看SLAVE STATUS;

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************  Slave_IO_State:     Master_Host: 192.168.73.110 Master_User: repluser   Master_Port: 3306   Connect_Retry: 10   Master_Log_File: mysql-bin.000003   Read_Master_Log_Pos: 245    Relay_Log_File: mariadb-relay-bin.000001    Relay_Log_Pos: 4    Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: No    Slave_SQL_Running: No   Replicate_Do_DB:
#此处信息已经改为正确

5. 重新启动线程

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

6. 再次查看SLAVE STATUS;

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************  Slave_IO_State: Waiting for master to send event    Master_Host: 192.168.73.110 Master_User: repluser   Master_Port: 3306   Connect_Retry: 10   Master_Log_File: mysql-bin.000003   Read_Master_Log_Pos: 7384     #已经有数据复制过来了   Relay_Log_File: mariadb-relay-bin.000002    Relay_Log_Pos: 7668 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes   Slave_SQL_Running: Yes
#IO和SQL线程已经启动

7. 查看下从节点内的库是否已经同步

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |          #hellodb库已经从主节点中复制过来了
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)
  • 其他说明:

如果生产中,发生主从节点之间的数据偏差较大并且迟迟不能同步,可以考虑将从服务器全部清除从新配置从服务器。

关于sql_slave_skip_counter的使用方法

当发生主键冲突时,从服务器会卡在出错的位置不再进行服务,此种错误一般会出现在主主复制或者从服务器已经占用了某条记录的情况下,此时可以使用此选项来忽略错误。

构建错误

此处继续沿用刚才的主从复制环境

1. 在从服务器上创建一条记录

MariaDB [(none)]> INSERT hellodb.teachers VALUE (5,'Li Xiaolong',30,'M');
Query OK, 1 row affected (0.00 sec)


2. 在主服务器上也创建一条主键相同的记录

MariaDB [(none)]> INSERT hellodb.teachers VALUE (5,'Xiao Yan',20,'M');
Query OK, 1 row affected (0.00 sec)

3. 返回从节点查看SLAVE STATUS

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************  Slave_IO_State: Waiting for master to send event    Master_Host: 192.168.73.110 Master_User: repluser   Master_Port: 3306   Connect_Retry: 10   Master_Log_File: mysql-bin.000003   Read_Master_Log_Pos: 7576   Relay_Log_File: mariadb-relay-bin.000002    Relay_Log_Pos: 7668 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes   Slave_SQL_Running: No   Replicate_Do_DB:    Replicate_Ignore_DB:    Replicate_Do_Table:     Replicate_Ignore_Table:     Replicate_Wild_Do_Table:    Replicate_Wild_Ignore_Table:    Last_Errno: 1062    Last_Error: Could not execute Write_rows event on table hellodb.teachers; Duplicate entry '5' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000003, end_log_pos 7549  Skip_Counter: 0 Exec_Master_Log_Pos: 7384   Relay_Log_Space: 8156   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: 1062    Last_SQL_Error: Could not execute Write_rows event on table hellodb.teachers; Duplicate entry '5' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000003, end_log_pos 7549  Replicate_Ignore_Server_Ids:    Master_Server_Id: 1
1 row in set (0.00 sec)

4. 从节点已经出错,在主节点继续添加记录

MariaDB [(none)]> INSERT hellodb.teachers VALUE (6,'Xiao Xuner',20,'M');
Query OK, 1 row affected (0.00 sec)

5. 此时从节点已经不会再继续从主节点复制信息

MariaDB [(none)]> SELECT * FROM hellodb.teachers WHERE tid>4;
+-----+-------------+-----+--------+
| TID | Name      | Age |Gender |
+-----+-------------+-----+--------+
|   5 | Li Xiaolong |  30 | M      |   #此为刚才从节点添加的记录
+-----+-------------+-----+--------+
1 row in set (0.00 sec)

排错

1. 使用sql_slave_skip_counter变量忽略错误

MariaDB [(none)]> SET GLOBAL sql_slave_skip_counter=1;
Query OK, 0 rows affected (0.00 sec)

2. 停止线程并重新启动

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)    MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

3. 查看slave status状态,此时已经没有报错的信息

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************  Slave_IO_State: Waiting for master to send event    Master_Host: 192.168.73.110 Master_User: repluser   Master_Port: 3306   Connect_Retry: 10   Master_Log_File: mysql-bin.000003   Read_Master_Log_Pos: 7770   Relay_Log_File: mariadb-relay-bin.000003    Relay_Log_Pos: 529  Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes   Slave_SQL_Running: Yes  Replicate_Do_DB:    Replicate_Ignore_DB:    Replicate_Do_Table:     Replicate_Ignore_Table:     Replicate_Wild_Do_Table:    Replicate_Wild_Ignore_Table:    Last_Errno: 0   Last_Error:     Skip_Counter: 0 Exec_Master_Log_Pos: 7770   Relay_Log_Space: 8634   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: 0
Master_SSL_Verify_Server_Cert: No   Last_IO_Errno: 0    Last_IO_Error:  Last_SQL_Errno: 0   Last_SQL_Error:     Replicate_Ignore_Server_Ids:    Master_Server_Id: 1
1 row in set (0.00 sec)

4. 在从服务器上查看teachers表

 MariaDB [(none)]> SELECT * FROM hellodb.teachers WHERE tid>4;
+-----+-------------+-----+--------+
| TID | Name        | Age | Gender |
+-----+-------------+-----+--------+
|   5 | Li Xiaolong |  30 | M      |
|   6 | Xiao Xuner  |  20 | M      |    #此时刚才在主节点插入的6号记录已经复制过来
+-----+-------------+-----+--------+
2 rows in set (0.00 sec)

以上为主从复制时出错的一些相关的修复方法,如果有帮助,感谢分享+在看;大家对什么内容感兴趣,也欢迎大家在留言区评论哦。

出处:https://www.linuxidc.com/Linux/2019-05/158646p2.htm

编辑:尹文敏

公司简介 | 恩墨学院 | 招聘 | DTCC | 数据技术嘉年华 | 免费课程 | 入驻华为严选商城

zCloud | SQM | Bethune Pro2 | zData一体机 | Mydata一体机 | ZDBM 备份一体机

Oracle技术架构 | 免费课程 | 数据库排行榜 | DBASK问题集萃 | 技术通讯

升级迁移 | 性能优化 | 智能整合 | 安全保障 |

云和恩墨大讲堂 | 一个分享交流的地方

长按,识别二维码,加入万人交流社群

请备注:云和恩墨大讲堂

解决方案 | MySQL DBA主从复制出错怎么办?相关推荐

  1. MySQL DBA亲授MySQL InnoDB事务ACID实现原理

    说到数据库事务,想到的就是要么都做修改,要么都不做,或者是 ACID 的概念.其实事务的本质就是锁.并发和重做日志的结合体. 这一篇主要讲一下 InnoDB 中的事务到底是如何实现 ACID 的: 原 ...

  2. MySQL Replication 主从复制全方位解决方案

    MySQL Replication 主从复制全方位解决方案 参考文章: (1)MySQL Replication 主从复制全方位解决方案 (2)https://www.cnblogs.com/clsn ...

  3. mysql高可靠部署_可能是我见过最好的 MySQL 高可用解决方案 MySQL InnoDB Cluster 中文教程!...

    公众号关注 「运维之美」设为「星标」,每天带你玩转 Linux ! 这篇文章将详细地介绍 MySQL 的高可用解决方案-- MySQL InnoDB Cluster. 说到高可用性,首先要了解一下什么 ...

  4. php mysql 主从复制_Windows 环境下,MySQL 的主从复制和主主复制

    Mysql的主从配置 1.找到配置文件 找到配置文件是主从复制的第一个难点.很多新手都容易找错配置文件,一般my.ini配置文件所在的位置都是隐藏的. 一般人都以为配置文件为 C:\Program F ...

  5. mysql之主从复制

    MySQL主从复制介绍 MySQL的主从复制是其自带的功能,通过逻辑的binlog日志复制到要同步的服务器本地,主服务器(Master),接收来自用户的内容更新,而一个或多个其他的服务器充当从服务器( ...

  6. mysql dba视频课_MySQL DBA专家

    该课程从零基础-专家级使用和精通Mysql数据库,可从事Mysql DBA工程师职位. 招聘需求如下: 培训目标: 一.Linux基础知识 二.Mysql基础 三.关系数据模型.字符集.常用的SQL语 ...

  7. db mysql / mysql dba / mysql manual / mysql config / mysql innotop

    MySQL 5.1 Reference Manual http://dev.mysql.com/doc/refman/5.1/en/ Including MySQL Cluster NDB 6.X/7 ...

  8. 2017版MySQL DBA核心课程-第1-16部完整-老男孩-专题视频课程

    2017版MySQL DBA核心课程-第1-16部完整-12443人已学习 课程介绍         linux培训班部分内容,培训班学员无需购买 2017版老男孩MySQL数据库视频课程-大集合 2 ...

  9. MySQL数据库主从复制与读写分离(图文详解!)

    目录 前言 一:MySQL数据库主从复制与读写分离 1.什么是读写分离? 2.为什么要读写分离呢? 3.什么时候要读写分离? 4.主从复制与读写分离 5.mysql支持的复制类型 (1)STATEME ...

最新文章

  1. koa连接mysql_CentOS 环境 Node + Koa2 连接 MySQL (ECS系列三)
  2. 禁用win10触摸屏手势_我才发现win10居然有这么多好用的功能
  3. Lucene学习总结之四:Lucene索引过程分析
  4. WPF 如何实现颜色值拾取
  5. Hibernate主键生成策略与save()方法是否发sql语句的研究
  6. sqlplus 小记
  7. 解读:计数器Counter
  8. java 弹幕游戏_java弹幕小游戏1.0版本
  9. 移动端自适应布局方案尝试
  10. 模拟电路和数字电路区别
  11. Pr 视频效果:图像控制、实用程序
  12. 用Python写了一个带界面的聊天室
  13. SpringBoot注解校验validation自定义异常返回错误消息给前端
  14. 发现同义词 python_同义词查找算法
  15. 云开发平台开源应用中心一次体验
  16. intellij创建快捷方式到桌面
  17. this指向,并改变this指向
  18. 1.1 机器学习和深度学习综述
  19. 正则表达式 密码 需包含字母数字特殊字符
  20. 找不到凭据分配oracle修正,远程连接身份验证错误,又找不到加密Oracle修正

热门文章

  1. 在linux中的文件中查找_如何在Linux中查找文件
  2. 适合初学者的安卓开源项目_开源周初学者
  3. (17)Node.js第三方模块
  4. 高性能网站建设指南——网站优化的14条建议
  5. 新特性 | Java8 的这个特性,用起来真的很爽!
  6. 微信小程序自定义组件(二)
  7. Bootstrap CSS编码规范之代码组织规范
  8. 休息是为了更好的出发
  9. 360团队 临时目录的原始文件不是360合法文件_谈谈腾讯电脑管家小团队版
  10. Windows 平台下基于MinGW和Qt 的OpenCV 之CMake 项目配置