使用过Mysql数据库朋友,一定听过读写分离,听的多的,估计耳朵都起茧子了。那么读写分离是怎么实现的呢,最常见的方法就是搭建Mysql的主从复制,主库提供写操作,从库提供读操作,从而达到应用的读写分离。

主从复制最常见的2种错误
第一种:主键冲突(Error_code: 1062)
第二种:记录丢失,例如update,delete操作,在从库找不到对应记录(Error_code: 1032)

下面来详细模拟一下记录丢失,处理全过程
检查主从复制是否正常

[root@localhost] 11:34:29 [testdb]>show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.0.1Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: binlog.000029Read_Master_Log_Pos: 3683Relay_Log_File: mysql-relay-bin.000003Relay_Log_Pos: 2207Relay_Master_Log_File: binlog.000029Slave_IO_Running: YesSlave_SQL_Running: Yes

可以看到IO线程和SQL线程运行都是正常的。

创建测试表和记录

[root@localhost] 11:25:48 [testdb]>show create table test1\G;
*************************** 1. row ***************************Table: test1
Create Table: CREATE TABLE `test1` (`id` int(11) NOT NULL,`name1` char(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',`name2` char(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.07 sec)insert into test1 values(1,'test1','test1');
insert into test1 values(2,'test2','test2');
insert into test1 values(3,'test3','test3');

模拟主从复制由于从库记录缺失,导致主从复制失败

第一步:在从库中删除id=2的记录

[root@localhost] 11:26:41 [testdb]>delete from test1 where id=2;
Query OK, 1 row affected (0.44 sec)[root@localhost] 11:26:52 [testdb]>select * from test1;
+----+-------+-------+
| id | name1 | name2 |
+----+-------+-------+
|  1 | test1 | test1 |
|  3 | test3 | test3 |
+----+-------+-------+
2 rows in set (0.00 sec)

第二步:在主库上删除id=2的记录

[root@localhost] 11:27:11 [testdb]>delete from test1 where id=2;
Query OK, 1 row affected (0.17 sec)[root@localhost] 11:27:51 [testdb]>select * from test1;
+----+-------+-------+
| id | name1 | name2 |
+----+-------+-------+
|  1 | test1 | test1 |
|  3 | test3 | test3 |
+----+-------+-------+
2 rows in set (0.00 sec)

在从库上查看主从复制情况

[root@localhost] 11:34:05 [testdb]>show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.0.1Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: binlog.000029Read_Master_Log_Pos: 3683Relay_Log_File: mysql-relay-bin.000003Relay_Log_Pos: 1929Relay_Master_Log_File: binlog.000029Slave_IO_Running: YesSlave_SQL_Running: NoReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 1032Last_Error: Could not execute Delete_rows event on table testdb.test1; Can't find record in 'test1', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log binlog.000029, end_log_pos 3652Skip_Counter: 0Exec_Master_Log_Pos: 3405Relay_Log_Space: 2414Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 1032Last_SQL_Error: Could not execute Delete_rows event on table testdb.test1; Can't find record in 'test1', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log binlog.000029, end_log_pos 3652Replicate_Ignore_Server_Ids:Master_Server_Id: 111213106Master_UUID: 3ada166e-c4db-11ea-b21d-000c29cc2388Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State:Master_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp: 200904 11:33:10Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set: 3ada166e-c4db-11ea-b21d-000c29cc2388:84830-84835Executed_Gtid_Set: 3ada166e-c4db-11ea-b21d-000c29cc2388:1-84834,
3ada166e-c4db-11ea-b21d-000c29cc2389:1-4Auto_Position: 1Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:
1 row in set (0.00 sec)

此时主从的sql线程已经是停止状态,主从复制的数据已经不同步了。复制开始报1032错误了。

要解决1032错误,可以有以下3中方案

**方案一:**手工将缺失的业务记录在主库上导出,并导入到从库,然后启动从库的sql线程就可以了。慢着,大家有没有注意到一个问题,就是在主库上,到底要导出哪条记录,报错信息里并没有,但是有提示,he event’s master log binlog.000029, end_log_pos 3652,所以还需要将binlog日志里的内容解析处理,找到要操作的记录,似乎有些麻烦。不用慌,还有方案二,方案三。

**方案二:**Mysql数据库提供一个参数slave_skip_errors,这个参数可以跳过指定错误代码的sql语句,例如:slave_skip_errors=1032,可惜,这个参数不能在线修改,修改生效需要重启实例,是不是也太友好。

[root@localhost] 11:28:57 [testdb]>set global slave_skip_errors=1032;
ERROR 1238 (HY000): Variable 'slave_skip_errors' is a read only variable

方案三:使用percona-toolkits工具集中的pt-slave-restart工具,自动跳过主从同步指定的报错代码sql语句,此方法对mysql数据侵入性小,不必重启Mysql实例

[mysql@mysql ~]$ pt-slave-restart --user=root --password=root --socket=/data/mysql/run/3306/mysql.sock --error-numbers=1032# A software update is available:
2020-09-04T11:32:07 S=/data/mysql/run/3306/mysql.sock,p=...,u=root mysql-relay-bin.000003        1651 1032

当跳过主从同步指定的报错代码sql语句,主从复制恢复之后,间隔64秒,会再次自动检测主从复制是否有1032错误。

其它类似的错误,都可以用以上三种方法方案处理,建议大家使用方案三

实战:一文带你解决Mysql主从复制日常错误相关推荐

  1. mysql 导出dmp文件_一文带你了解MySQL主从复制(Master-Slave)

    1.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...

  2. 重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor

    重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...

  3. 重置密码解决MySQL for Linux错误 ERROR 1045 (28000):

    重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...

  4. mysql unrecognized_service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误...

    service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误的方法如下: [root@ctohome.com ~]# service ...

  5. 3分钟解决MySQL 1032 主从错误(转)

    转自  https://blog.51cto.com/suifu/1845457 3分钟解决MySQL 1032主从错误 Part1:写在最前 1032错误----现在生产库中好多数据,在从库误删了, ...

  6. 3分钟解决MySQL 1032 主从错误

    3分钟解决MySQL 1032主从错误 Part1:写在最前 1032错误----现在生产库中好多数据,在从库误删了,生产库更新后找不到了,现在主从不同步了,再跳过错误也没用,因为没这条,再更新还会报 ...

  7. mysql主从1594错误_3分钟解决MySQL主从1594错误

    3分钟解决MySQL主从1594错误简介 Part1:写在最前 1594这个错误看起来挺严重的,会提示你binlog文件或者Relay log损坏了,例如binary log is corrupted ...

  8. mysql 1032错误_如何快速解决MySQL 1032 主从错误

    3分钟解决MySQL 1032主从错误 Part1:写在最前 1032错误----现在生产库中好多数据,在从库误删了,生产库更新后找不到了,现在主从不同步了,再跳过错误也没用,因为没这条,再更新还会报 ...

  9. mysql 1032错误_3分钟解决MySQL 1032 主从错误

    wKioL1gapS3yFcPpAAA4eVx2Dz8496.jpg 3分钟解决MySQL 1032主从错误 Part1:写在最前 1032错误----现在生产库中好多数据,在从库误删了,生产库更新后 ...

最新文章

  1. Django博客系统(首页分类数据展示)
  2. Matlab中plot函数全功能解析
  3. mysql5.6 icp mrr bak_【mysql】关于ICP、MRR、BKA等特性
  4. python田字格的输出的两种方法
  5. 【NOI2015】品酒大会【后缀数组】【并查集】
  6. Spring Boot笔记-IDEA使用JPA映射时解决cannot resolve column或cannot resolve table
  7. 网约车司机用橡胶棍追打女乘客被行政拘留
  8. ios项目 swift 定义常量 其他文件引用_面试应该注意的Swift知识点
  9. LightOJ 1245 - Harmonic Number (II)
  10. python有什么内容_python的类(简介,没什么内容)
  11. 【机器学习】xgboost以及lightgbm资料汇总
  12. 使用MSDN学习ASP.NET的工作流程
  13. 数学建模之预测方法总结与案例
  14. 大数据与人工智能方向基础课程简单介绍
  15. 数字万用表判断绝缘栅场效应管的好坏
  16. python_计算股票指标
  17. python如何期货交易_Python期货量化交易实战
  18. 如何两个电脑共享文件实现多人编辑_excel怎么实现多人共同编辑一个文档
  19. hivesql失败告警发送到企业微信
  20. 教师计算机考试模块有哪些内容有哪些内容,教师资格考试信息与信息技术模块知识点...

热门文章

  1. 最新(2019)斯坦福CS224n深度学习自然语言处理课程(视频+笔记+2017年合集)
  2. 2019工作榜单|Python程序员吸金榜,AI排第一,这个我服!
  3. mysql执行语句_MySQL查看实时执行的SQL语句
  4. Linux内核移植漫谈——你不是第一个想移植Linux内核的人
  5. 怎么安装python3.7 setuptools与pip_python3之安装、pip、setuptools
  6. Algs4-1.2.8引用型变量赋值-数组复制
  7. python【第三篇】迭代器、生成器、闭包
  8. IO多路复用(番外篇)、poll、epoll三者的区别
  9. 滚动监听 after选择器
  10. swiper在微信端滑动效果不友好(滑动不了)的解决方案