1. MySQL 数据恢复常用办法

MySQL恢复的方法一般有三种:

1. 官方推荐的基于全备+binlog , 通常做法是先恢复最近一次的全备,然后通过mysqlbiinlog --start-position --stop-position binlog.000xxx | mysql -uroot -p xxx -S database 恢复到目标数据库做恢复

2. 基于主从同步恢复数据,通常做法是先恢复最近一次的全备,然后恢复后的实例做slave 挂载到现有的master 上面,通过 start slave sql_thread until master_log_pos 恢复到故障前的一个pos。

现在尝试第三种恢复方式, 通过原来主库上面的binlog 把数据都恢复到slave 上。

处理思路: 

因为relaylog和binlog本质实际上是一样的,所以是否可以利用MySQL自身的sql_thread来增量binlog

1)重新初始化一个实例,恢复全量备份文件。
    2)找到第一个binlog文件的position,和剩下所有的binlog。
    3)将binlog伪装成relaylog,通过sql thread增量恢复。

应用场景:

1. 最近的一次全备离故障位置比较远,通过上面两种方式的恢复时间太慢

2. 双主keepalived的集群,由于keepalived没有像MHA 那样有日志补全机制,出故障是有可能会有数据丢失的,万一同步有严重的复制延时出现故障切换到slave,这样数据就不一致,需要做日志补全

2. 实验步骤

1. 建立基于主从同步(这里实验基于传统的pos, 其实GTID 也一样可行)

M1 :

  1. root@localhost:mysql3307.sock [(none)]>select * from restore.t1;
  2. +----+------+
  3. | id | c1 |
  4. +----+------+
  5. | 1 | 1 |
  6. | 2 | 3 |
  7. | 3 | 2 |
  8. | 4 | 3 |
  9. | 5 | 6 |
  10. | 6 | 7 |
  11. | 7 | 9 |
  12. | 10 | NULL |
  13. | 11 | 10 |
  14. +----+------+
  15. 9 rows in set (0.00 sec)

 M2:(slave)

  1. root@localhost:mysql3307.sock [(none)]>select * from restore.t1;
  2. +----+------+
  3. | id | c1 |
  4. +----+------+
  5. | 1 | 1 |
  6. | 2 | 3 |
  7. | 3 | 2 |
  8. | 4 | 3 |
  9. | 5 | 6 |
  10. | 6 | 7 |
  11. | 7 | 9 |
  12. | 10 | NULL |
  13. | 11 | 10 |
  14. +----+------+
  15. 9 rows in set (0.00 sec)

  

  1. root@localhost:mysql3307.sock [restore]>show slave status\G
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: m1
  5. Master_User: repl
  6. Master_Port: 3307
  7. Connect_Retry: 60
  8. Master_Log_File: 3307-binlog.000002
  9. Read_Master_Log_Pos: 154
  10. Relay_Log_File: M2-relay-bin.000004
  11. Relay_Log_Pos: 371
  12. Relay_Master_Log_File: 3307-binlog.000002
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 154
  25. Relay_Log_Space: 624
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 13307
  43. Master_UUID: afeab8d6-b871-11e7-9b2a-005056b643b3
  44. Master_Info_File: /data/mysql/3307/data/master.info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. Replicate_Rewrite_DB:
  58. Channel_Name:
  59. Master_TLS_Version:
  60. 1 row in set (0.00 sec)

 记录此时slave 的 relay-log 信息

  1. [root@M2 data]# more M2-relay-bin.index
  2. ./M2-relay-bin.000003
  3. ./M2-relay-bin.000004
  4.  
  5. [root@M2 data]# more relay-log.info
  6. 7
  7. ./M2-relay-bin.000004
  8. 371
  9. 3307-binlog.000002
  10. 154
  11. 0
  12. 0
  13. 1

 2. 使用sysbench 模拟数据不同步

  1. [root@M1 logs]# mysqladmin create sbtest
  1. [root@M1 sysbench]# sysbench --db-driver=mysql --mysql-host=m1 --mysql-port=3307 --mysql-user=sbtest --mysql-password='sbtest' /usr/share/sysbench/oltp_common.lua --tables=4 --table-size=100000 --threads=2 --time=60 --report-interval=10 prepare

  在主库导入数据的时候在slave端停止同步,制造数据不一致

  1. root@localhost:mysql3307.sock [mysql]>stop slave

 3. 等sysbench执行完,查看主库的数据和slave 的数据

主库:

  1. root@localhost:mysql3307.sock [sbtest]>select count(1) from sbtest1;
  2. +----------+
  3. | count(1) |
  4. +----------+
  5. | 100000 |
  6. +----------+
  7. 1 row in set (0.05 sec)
  8.  
  9. root@localhost:mysql3307.sock [sbtest]>select count(1) from sbtest2;
  10. +----------+
  11. | count(1) |
  12. +----------+
  13. | 100000 |
  14. +----------+
  15. 1 row in set (0.05 sec)
  16.  
  17. root@localhost:mysql3307.sock [sbtest]>select count(1) from sbtest3;
  18. +----------+
  19. | count(1) |
  20. +----------+
  21. | 100000 |
  22. +----------+
  23. 1 row in set (0.05 sec)
  24.  
  25. root@localhost:mysql3307.sock [sbtest]>select count(1) from sbtest4;
  26. +----------+
  27. | count(1) |
  28. +----------+
  29. | 100000 |
  30. +----------+
  31. 1 row in set (0.05 sec)

  slave 端:

  1. root@localhost:mysql3307.sock [sbtest]>select count(1) from sbtest4;
  2. +----------+
  3. | count(1) |
  4. +----------+
  5. | 67550 |
  6. +----------+
  7. 1 row in set (0.06 sec)
  8.  
  9. root@localhost:mysql3307.sock [sbtest]>select count(1) from sbtest3;
  10. +----------+
  11. | count(1) |
  12. +----------+
  13. | 70252 |
  14. +----------+
  15. 1 row in set (0.04 sec)

  可以看到主从不同步。

4. 此时查看slave 的status:

  1. root@localhost:mysql3307.sock [(none)]>show slave status\G
  2. *************************** 1. row ***************************
  3. Slave_IO_State:
  4. Master_Host: m1
  5. Master_User: repl
  6. Master_Port: 3307
  7. Connect_Retry: 60
  8. Master_Log_File: 3307-binlog.000002
  9. Read_Master_Log_Pos: 76364214
  10. Relay_Log_File: M2-relay-bin.000004
  11. Relay_Log_Pos: 64490301
  12. Relay_Master_Log_File: 3307-binlog.000002
  13. Slave_IO_Running: No
  14. Slave_SQL_Running: No
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 64490084
  25. Relay_Log_Space: 76364861
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: NULL
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 0
  43. Master_UUID: afeab8d6-b871-11e7-9b2a-005056b643b3
  44. Master_Info_File: /data/mysql/3307/data/master.info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State:
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. Replicate_Rewrite_DB:
  58. Channel_Name:
  59. Master_TLS_Version:
  60. 1 row in set (0.00 sec)

 由于本地的relay log 没有执行完毕,为了保证实验准确性,我们先让本地的relaylog 执行完 , start slave sql_thread

再次检查:

  1. *************************** 1. row ***************************
  2. Slave_IO_State:
  3. Master_Host: m1
  4. Master_User: repl
  5. Master_Port: 3307
  6. Connect_Retry: 60
  7. Master_Log_File: 3307-binlog.000002
  8. Read_Master_Log_Pos: 76364214
  9. Relay_Log_File: M2-relay-bin.000005
  10. Relay_Log_Pos: 4
  11. Relay_Master_Log_File: 3307-binlog.000002
  12. Slave_IO_Running: No
  13. Slave_SQL_Running: Yes
  14. Replicate_Do_DB:
  15. Replicate_Ignore_DB:
  16. Replicate_Do_Table:
  17. Replicate_Ignore_Table:
  18. Replicate_Wild_Do_Table:
  19. Replicate_Wild_Ignore_Table:
  20. Last_Errno: 0
  21. Last_Error:
  22. Skip_Counter: 0
  23. Exec_Master_Log_Pos: 76364214
  24. Relay_Log_Space: 154
  25. Until_Condition: None
  26. Until_Log_File:
  27. Until_Log_Pos: 0
  28. Master_SSL_Allowed: No
  29. Master_SSL_CA_File:
  30. Master_SSL_CA_Path:
  31. Master_SSL_Cert:
  32. Master_SSL_Cipher:
  33. Master_SSL_Key:
  34. Seconds_Behind_Master: NULL
  35. Master_SSL_Verify_Server_Cert: No
  36. Last_IO_Errno: 0
  37. Last_IO_Error:
  38. Last_SQL_Errno: 0
  39. Last_SQL_Error:
  40. Replicate_Ignore_Server_Ids:
  41. Master_Server_Id: 0
  42. Master_UUID: afeab8d6-b871-11e7-9b2a-005056b643b3
  43. Master_Info_File: /data/mysql/3307/data/master.info
  44. SQL_Delay: 0
  45. SQL_Remaining_Delay: NULL
  46. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  47. Master_Retry_Count: 86400
  48. Master_Bind:
  49. Last_IO_Error_Timestamp:
  50. Last_SQL_Error_Timestamp:
  51. Master_SSL_Crl:
  52. Master_SSL_Crlpath:
  53. Retrieved_Gtid_Set:
  54. Executed_Gtid_Set:
  55. Auto_Position: 0
  56. Replicate_Rewrite_DB:
  57. Channel_Name:
  58. Master_TLS_Version:
  59. 1 row in set (0.00 sec)

  本地relaylog 已经全部执行完毕,此时记录最新的relay log 信息:

  1. [root@M2 data]# more relay-log.info
  2. 7
  3. ./M2-relay-bin.000005
  4. 4
  5. 3307-binlog.000002
  6. 76364214
  7. 0
  8. 0
  9. 1
  10.  
  11. 0
  12. 0
  13. 1

  上面这个信息很重要,说明了从库执行到主库的000002 的binlog的76364214 这个位置,我们下面将主库的binlog 拷贝过来模拟relaylog, 并从这个位置开始恢复

5. 拷贝binlog 到目标端,并模拟成relay log

拷贝前先关闭从库,并修改cnf (skip-slave-start)让slave 不会重启后自动开始复制

  1. [root@M2 data]# ll
  2. total 185248
  3. -rw-r----- 1 root root 461 Oct 24 17:14 3307-binlog.000001
  4. -rw-r----- 1 root root 76364609 Oct 24 17:14 3307-binlog.000002
  5. -rw-r----- 1 root root 203 Oct 24 17:14 3307-binlog.000003
  6. -rw-r----- 1 root root 419 Oct 24 17:14 3307-binlog.000004
  7. -rw-r----- 1 root root 164 Oct 24 17:14 3307-binlog.index
  8. -rw-r----- 1 mysql mysql 56 Oct 24 15:08 auto.cnf
  9. -rw-r----- 1 mysql mysql 4720 Oct 24 17:14 ib_buffer_pool
  10. -rw-r----- 1 mysql mysql 12582912 Oct 24 17:14 ibdata1
  11. -rw-r----- 1 mysql mysql 50331648 Oct 24 17:14 ib_logfile0
  12. -rw-r----- 1 mysql mysql 50331648 Oct 24 17:11 ib_logfile1
  13. -rw-r----- 1 mysql mysql 177 Oct 24 17:14 M2-relay-bin.000005
  14. -rw-r----- 1 mysql mysql 22 Oct 24 17:11 M2-relay-bin.index
  15. -rw-r----- 1 mysql mysql 122 Oct 24 17:14 master.info
  16. drwxr-x--- 2 mysql mysql 4096 Oct 24 15:07 mysql
  17. -rw------- 1 root root 0 Oct 24 15:08 nohup.out
  18. drwxr-x--- 2 mysql mysql 4096 Oct 24 15:07 performance_schema
  19. -rw-r----- 1 mysql mysql 68 Oct 24 17:14 relay-log.info
  20. drwxr-x--- 2 mysql mysql 4096 Oct 24 15:07 restore
  21. drwxr-x--- 2 mysql mysql 4096 Oct 24 16:47 sbtest
  22. drwxr-x--- 2 mysql mysql 12288 Oct 24 15:07 sys
  23. -rw-r----- 1 mysql mysql 24 Oct 24 15:07 xtrabackup_binlog_pos_innodb
  24. -rw-r----- 1 mysql mysql 577 Oct 24 15:07 xtrabackup_info

 改名为relay log

  1. [root@M2 data]# cp 3307-binlog.000001 relay.000001
  2. [root@M2 data]# cp 3307-binlog.000002 relay.000002
  3. [root@M2 data]# cp 3307-binlog.000003 relay.000003
  4. [root@M2 data]# cp 3307-binlog.000004 relay.000004 
  1. 改权限属性
  1. [root@M2 data]# chown mysql.mysql -R *

 修改relay log index 文件,让系统能识别

  1. [root@M2 data]# cat M2-relay-bin.index
  2. ./relay.000001
  3. ./relay.000002
  4. ./relay.000003
  5. ./relay.000004

 修改relay log info 文件,告诉系统从哪个位置开始复制

  1. [root@M2 data]# cat relay-log.info
  2. 7
  3. ./relay.000002
  4. 76364214
  5. 3307-binlog.000002

  6. 0
  7. 0
  8. 1
  9.  
  10. 0
  11. 0
  12. 1

 最后开起sql_thread 进程开始快速恢复

  1. start slave sql_thread

 6. 检查数据是否一致

slave:

  1. oot@localhost:mysql3307.sock [sbtest]>select count(1) from sbtest4;
  2. +----------+
  3. | count(1) |
  4. +----------+
  5. | 100000 |
  6. +----------+
  7. 1 row in set (0.05 sec)
  8.  
  9. root@localhost:mysql3307.sock [sbtest]>select count(1) from sbtest3;
  10. +----------+
  11. | count(1) |
  12. +----------+
  13. | 100000 |
  14. +----------+
  15. 1 row in set (0.05 sec)

 可以看到slave 已经把缺失的数据都全部恢复了。

  1.  

MySQL 5.7 基于复制线程SQL_Thread加快恢复的尝试的更多相关文章

  1. (转)通过MySQL复制线程SQL_Thread加快增量恢复binlog

    数据回档常常是使用全量备份+binlog增量实现的.而数据量很大的情况下,增量恢复binlog一直是一个苦恼的问题,因为恢复binlog速度十分慢,并且容易出错. 恢复binlog文件一般有两种方法: ...

  2. Mysql 5.7 基于组复制(MySQL Group Replication) - 运维小结

    之前介绍了Mysq主从同步的异步复制(默认模式).半同步复制.基于GTID复制.基于组提交和并行复制 (解决同步延迟),下面简单说下Mysql基于组复制(MySQL Group Replication ...

  3. MySQL · 引擎特性 · 基于InnoDB的物理复制实现(转载)

    http://mysql.taobao.org/monthly/2016/05/01/ 在开始之前,你需要对InnoDB的事务系统有个基本的认识.如果您不了解,可以参考我之前的几篇关于InnoDB的文 ...

  4. MySQL 5.7基于组提交的并行复制

    参考链接: http://mysql.taobao.org/monthly/2016/08/01/ https://www.kancloud.cn/thinkphp/mysql-parallel-ap ...

  5. MySQL 5.7 基于GTID主从复制+并行复制+半同步复制

    环境准备 IP HOSTNAME SERVICE SYSTEM 192.168.131.129 mysql-master1 mysql CentOS7.6 192.168.131.130 mysql- ...

  6. MySQL 5.7基于GTID复制的常见问题和修复步骤(二)

    [问题二] 有一个集群(MySQL5.7.23)切换后复制slave报1236,其实是不小心在slave上执行了事务导致 Got fatal error 1236 from master when r ...

  7. [MySQL Reference Manual] 18 复制

    18 复制 18 复制 18.1 复制配置 18.1.1 基于Binary Log的数据库复制配置 18.1.2 配置基于Binary log的复制 18.1.2.1 设置复制master的配置 18 ...

  8. Mysql主从同步(复制)

    目录: mysql主从同步定义      主从同步机制 配置主从同步      配置主服务器      配置从服务器 使用主从同步来备份      使用mysqldump来备份      备份原始文件 ...

  9. Mysql主从同步(复制)(转)

    文章转自:https://www.cnblogs.com/kylinlin/p/5258719.html 目录: mysql主从同步定义 主从同步机制 配置主从同步 配置主服务器 配置从服务器 使用主 ...

随机推荐

  1. POJ-1256 next_permutation函数应用

    字典序列: 在字典序中蕴含着一个点,就是大小的问题,谁先出现,谁后出现的问题.譬如a<b<c,出现顺序就是a,b,c. 本题中字符集是所有大小写字母,而题目中规定的谁大谁小已经不是按asc ...

  2. 【Python】 压缩文件处理 zipfile & tarfile

    [zipfile] 虽然叫zipfile,但是除了zip之外,rar,war,jar这些压缩(或者打包)文件格式也都可以处理. zipfile模块常用的一些操作和方法: is_zipfile(file ...

  3. 设计模式 --> (9)代理模式

    代理模式 为其他对象提供一种代理以控制对这个对象的访问. 主要解决的问题是:在直接访问对象时带来的问题,比如说:要访问的对象在远程的机器上.在面向对象系统中,有些对象由于某些原因(比如对象创建开销很大 ...

  4. Algorithm --> 棋盘中求出A到B的最小步数

    求出A到B的最小步数 给定象棋盘,以及位置A和B, 求出从A到B的最小步数 代码: #include <cstdio> #include <iostream> #include ...

  5. java性能调优---------------------JVM调优方案

    JVM的调优的主要过程有: 1.确定堆内存大小(-Xmx.-Xms) 2.合理分配新生代和老年代(-XX:NewRatio.-Xmn.-XX:SurvivorRatio) 3.确定永久区大小(-XX: ...

  6. 自动化制作.framework

    1.生成.framework前的配置工作详见:http://www.cnblogs.com/huangzs/p/8029258.html 2. 将以下脚本粘贴进去,修改FMK_NAME. p.p1 { ...

  7. jquery datatable ajax配置详解

    我写的这个东西类似于个人笔记,如果你想要完整的而了解 可以去这里看看 http://dt.thxopen.com/ 包括英文原网站都不错. 通过配置ajax的属性和服务器交互 $("sele ...

  8. Git详细教程(2)---多人协作开发

    Git可以完成两件事情: 1. 版本控制 2.多人协作开发 如今的项目,规模越来越大,功能越来越多,需要有一个团队进行开发. 如果有多个开发人员共同开发一个项目,如何进行协作的呢. Git提供了一个非 ...

  9. JavaScript判断类型

    1.typeof操作符,返回值为字符串,用来判断一个值是哪种基本类型 "undefined"-Undefined "boolean"-Boolean " ...

  10. 第五次作业-需求&原型改进

    需求&原型改进 0. 团队介绍 团队名称:121ComeOn 项目名称:个人博客项目 团队组成: PM:黄金筱(107) 成员:王枫(031),刘烨(255),周明浩(277) github地 ...