MySQL 中drop 等高危误操作后恢复方法

实验目的:

  • 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复.

方法:

1.环境

  • mysql 5.7.23-log
  • master db210(192.168.99.210)
  • slave db212(192.168.99.212)
  • 伪装master db210:3507
  • 临时slave:db211(192.168.99.211)
  • 压力:在做本次实验过程用,使用sysbench同时做读写模拟压力(28thread+10*100000行)

2.本实验的前置条件:

  • 在drop前至少有一次全量备份
  • 从全备到drop操作时间点有全程binlog,并且binlog format='row'
    如果没有以上两个备份怎么办?见《无备份的误操作恢复方法》

3.模拟误操作

root@localhost [wenyz]>select count(*) from t2;
+----------+
| count(*) |
+----------+
| 163818 |
+----------+
1 row in set (0.12 sec) root@localhost [wenyz]>drop table t2;
Query OK, 0 rows affected (0.02 sec) root@localhost [wenyz]>show master status;
+------------------+-----------+--------------+------------------+------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+-----------+--------------+------------------+------------------------------------------------+
| mysql-bin.000020 | 136445791 | | | 9e912d2b-92f6-11e8-9747-0050568be82c:1-9872111 |
+------------------+-----------+--------------+------------------+------------------------------------------------+
1 row in set (0.00 sec) root@localhost [wenyz]>flush logs;
Query OK, 0 rows affected (1.41 sec)

4.在实验不同方法之前先完成以下步骤:

  • 复制全备份和所有binlog文件到空闲服务器:db211上
  • 以全备恢复到一个临时新实例(临时slave).

5.找到误操作的位置(GTID或binlog pos)


通过查找怀疑的binlog:
[root@db210_19:11:07 /data/57mysql/mysql3506/logs]
#ll
total 75564544
-rw-r----- 1 mysql mysql 177 Jul 29 14:14 mysql-bin.000001
...
-rw-r----- 1 mysql mysql 39336996 Jul 29 19:12 mysql-bin.000020
-rw-r----- 1 mysql mysql 13340 Jul 29 19:12 mysql-bin.index
[root@db210_19:12:48 /data/57mysql/mysql3506/logs] binlog文件导出命令:
#mysqlbinlog -v --base64-output=auto /data/57mysql/mysql3506/logs/mysql-bin.000020 >/data/backup/20.sql
binlog文件导出后查找:DROP TABLE `t2`: 在mysql-bin.000020中找到对应位置:
# at 97871478
#180731 19:33:29 server id 1853506 end_log_pos 97871543 CRC32 0x8cd4354d GTID last_committed=205567 sequence_number=205570 rbr_only=no original_committed_timestamp=0 immediate_commit_timestamp=0 transaction_length=0
# original_commit_timestamp=0 (1970-01-01 08:00:00.000000 CST)
# immediate_commit_timestamp=0 (1970-01-01 08:00:00.000000 CST)
/*!80001 SET @@session.original_commit_timestamp=0*//*!*/;
SET @@SESSION.GTID_NEXT= '9e912d2b-92f6-11e8-9747-0050568be82c:9791090'/*!*/;
# at 97871543
#180731 19:33:29 server id 1853506 end_log_pos 97871660 CRC32 0x974a86d8 Query thread_id=35 exec_time=0 error_code=0
use `wenyz`/*!*/;
SET TIMESTAMP=1533036809/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
DROP TABLE `t2` /* generated by server */
/*!*/;
# at 97871660
#180731 19:33:29 server id 1853506 end_log_pos 97871725 CRC32 0xb85257d6 GTID last_committed=205567 sequence_number=205571 rbr_only=yes original_committed_timestamp=0 immediate_commit_timestamp=0 transaction_length=0
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=0 (1970-01-01 08:00:00.000000 CST)
# immediate_commit_timestamp=0 (1970-01-01 08:00:00.000000 CST)
/*!80001 SET @@session.original_commit_timestamp=0*//*!*/;
SET @@SESSION.GTID_NEXT= '9e912d2b-92f6-11e8-9747-0050568be82c:9791091'/*!*/;
# at 97871725

恢复方法二:

  • 初始化一个空实例(用于伪master),并配置同步账号:
[root@db210_00:22:09 /data/57mysql/mysql3507/data]
#/usr/local/mysql57/bin/mysqld --defaults-file=/data/57mysql/mysql3507/my3506.cnf --initialize
[root@db210_00:22:30 /data/57mysql/mysql3507/data]
#cat error.log |grep pass
2018-07-31T16:22:28.157717Z 1 [Note] A temporary password is generated for root@localhost: ?NqFa+vJZ9U_
[root@db210_00:22:45 /data/57mysql/mysql3507/data]
#/usr/local/mysql57/bin/mysqld --defaults-file=/data/57mysql/mysql3507/my3506.cnf&
[2] 19851
[root@db210_00:22:59 /data/57mysql/mysql3507/data]
#mysql -S /tmp/mysql3507.sock -p
Enter password:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. (unknown)@localhost [(none)]>alter user user() identified by 'xxxx';
Query OK, 0 rows affected (0.00 sec) root@localhost [(none)]>grant all on *.* to 'wyz'@'%' identified by 'xxxx';
Query OK, 0 rows affected, 1 warning (0.00 sec)
  • 关闭伪master实例
root@localhost [(none)]>shutdown;
Query OK, 0 rows affected (0.00 sec)
  • 复制binlog并伪装binlog环境

    • 删除初始binlog,复制备份binlog文件
    • 制作mysql-bin.index文件,并修改logs目录下所有文件权限为mysql:mysql
[root@db210_00:24:25 /data/57mysql/mysql3507/logs]
#ll
total 12
-rw-r----- 1 mysql mysql 177 Aug 1 00:22 mysql-bin.000001
-rw-r----- 1 mysql mysql 716 Aug 1 00:24 mysql-bin.000002
-rw-r----- 1 mysql mysql 92 Aug 1 00:23 mysql-bin.index
[root@db210_00:24:26 /data/57mysql/mysql3507/logs]
#rm -rf mysql-bin.0*
#ls /data/57mysql/mysql3507/logs/mysql-bin.0* >mysql-bin.index
#chown mysql:mysql * #cat mysql-bin.index
/data/57mysql/mysql3507/logs/mysql-bin.000010
/data/57mysql/mysql3507/logs/mysql-bin.000011
/data/57mysql/mysql3507/logs/mysql-bin.000012
/data/57mysql/mysql3507/logs/mysql-bin.000013
/data/57mysql/mysql3507/logs/mysql-bin.000014
/data/57mysql/mysql3507/logs/mysql-bin.000015
/data/57mysql/mysql3507/logs/mysql-bin.000016
/data/57mysql/mysql3507/logs/mysql-bin.000017
/data/57mysql/mysql3507/logs/mysql-bin.000018
/data/57mysql/mysql3507/logs/mysql-bin.000019
/data/57mysql/mysql3507/logs/mysql-bin.000020 [root@db210_00:29:28 /data/57mysql/mysql3507/logs]
#ll
total 2874136
-rw-r----- 1 mysql mysql 268435555 Aug 1 00:25 mysql-bin.000010
-rw-r----- 1 mysql mysql 268442595 Aug 1 00:25 mysql-bin.000011
-rw-r----- 1 mysql mysql 268442419 Aug 1 00:25 mysql-bin.000012
-rw-r----- 1 mysql mysql 268437857 Aug 1 00:25 mysql-bin.000013
-rw-r----- 1 mysql mysql 268438414 Aug 1 00:25 mysql-bin.000014
-rw-r----- 1 mysql mysql 268437131 Aug 1 00:25 mysql-bin.000015
-rw-r----- 1 mysql mysql 268436889 Aug 1 00:25 mysql-bin.000016
-rw-r----- 1 mysql mysql 268437011 Aug 1 00:25 mysql-bin.000017
-rw-r----- 1 mysql mysql 268441823 Aug 1 00:25 mysql-bin.000018
-rw-r----- 1 mysql mysql 268439288 Aug 1 00:25 mysql-bin.000019
-rw-r----- 1 mysql mysql 258700685 Aug 1 00:25 mysql-bin.000020
-rw-r----- 1 mysql mysql 506 Aug 1 00:25 mysql-bin.index
  • 启动伪装master实例,确认相关信息
    -注意show master status中有一个增加了eaba29d3-94dd-11e8-91bf-0050568be82c:1-2,需要在slave 中gtid purged掉,否则会报1236错误(因为刚才新建账号产生的log已经被删除了)
[root@db210_00:29:29 /data/57mysql/mysql3507/logs]
#/usr/local/mysql57/bin/mysqld --defaults-file=/data/57mysql/mysql3507/my3506.cnf&
[2] 19942
[root@db210_00:29:35 /data/57mysql/mysql3507/logs]
#mysql -S /tmp/mysql3507.sock -pxxxx
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. root@localhost [(none)]>show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000010 | 268435555 |
| mysql-bin.000011 | 268442595 |
| mysql-bin.000012 | 268442419 |
| mysql-bin.000013 | 268437857 |
| mysql-bin.000014 | 268438414 |
| mysql-bin.000015 | 268437131 |
| mysql-bin.000016 | 268436889 |
| mysql-bin.000017 | 268437011 |
| mysql-bin.000018 | 268441823 |
| mysql-bin.000019 | 268439288 |
| mysql-bin.000020 | 258700685 |
| mysql-bin.000021 | 194 |
+------------------+-----------+
12 rows in set (0.00 sec)
root@localhost [(none)]>show master status;
+------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------------+
| mysql-bin.000021 | 194 | | | 9e912d2b-92f6-11e8-9747-0050568be82c:1-10128895,
eaba29d3-94dd-11e8-91bf-0050568be82c:1-2 |
+------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • 在临时slave(db211)上change master to 到伪master上.
    gtid
    purged设置.9e912d2b-92f6-11e8-9747-0050568be82c:1-6730273为xtrabackup_info中备份时记录的,eaba29d3-94dd-11e8-91bf-0050568be82c:1-2为搭建伪master过程中建立账号产生的.
  root@localhost [(none)]>set global gtid_purged='9e912d2b-92f6-11e8-9747-0050568be82c:1-6730273,eaba29d3-94dd-11e8-91bf-0050568be82c:1-2';

  root@localhost [(none)]>start slave io_thread;
root@localhost [(none)]>start slave sql_thread until sql_before_gtids='9e912d2b-92f6-11e8-9747-0050568be82c:9791090';
root@localhost [(none)]>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: db210
Master_User: wyz
Master_Port: 3507
Connect_Retry: 60
Master_Log_File: mysql-bin.000021
Read_Master_Log_Pos: 194
Relay_Log_File: relay-bin.000024
Relay_Log_Pos: 97871691
Relay_Master_Log_File: mysql-bin.000020
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: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 97871478
Relay_Log_Space: 258702241
Until_Condition: SQL_BEFORE_GTIDS
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: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2103507
Master_UUID: eaba29d3-94dd-11e8-91bf-0050568be82c
Master_Info_File: /data/57mysql/mysql3506/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 9e912d2b-92f6-11e8-9747-0050568be82c:6730274-10128895
Executed_Gtid_Set: 9e912d2b-92f6-11e8-9747-0050568be82c:1-9791089,
eaba29d3-94dd-11e8-91bf-0050568be82c:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec) ERROR:
No query specified root@localhost [(none)]>select count(*) from wenyz.t2;
+----------+
| count(*) |
+----------+
| 163818 |
+----------+
1 row in set (0.11 sec)
  • 至此数据已经回来了.导入master的工作就不在重复,参照方法一中.

转自

通过全备+binlog_server同步恢复被drop的库或表 - 2森林 - 博客园 https://www.cnblogs.com/2woods/p/9394625.html

MySQL伪master+Binlog+同步【转】的更多相关文章

  1. MYSQL 的 MASTER到MASTER的主主循环同步

    MYSQL 的 MASTER到MASTER的主主循环同步   刚刚抽空做了一下MYSQL的主主同步.把步骤写下来,至于会出现的什么问题,以后随时更新.这里我同步的数据库是TEST1.环境描述.   主 ...

  2. Mysql 实现基于binlog的主从同步

    工作原理 1.主节点必须启用二进制日志,记录任何修改了数据库数据的事件.2.从节点开启一个线程(I/O Thread)把自己扮演成 mysql 的客户端,通过 mysql 协议,请求主节点的二进制日志 ...

  3. mysql主从复制实现数据库同步

    mysql主从复制相信已经用得很多了,但是由于工作原因一直没怎么用过.趁着这段时间相对空闲,也就自己实现一遍.尽管互联网上已有大把类似的文章,但是自身实现的仍然值得记录. 环境: 主服务器:cento ...

  4. Mysql数据库之Binlog日志使用总结

    binlog二进制日志对于mysql数据库的重要性有多大,在此就不多说了.下面根据本人的日常操作经历,并结合网上参考资料,对binlog日志使用做一梳理: 一.binlog日志介绍1)什么是binlo ...

  5. mysql数据库从库同步延迟的问题

    在从服务器上执行show slave status;可以查看到很多同步的参数,我们需要特别注意的参数如下,希望文章对各位会有所帮助. 在从服务器上执行show slave status;可以查看到很多 ...

  6. mysql (master/slave)复制原理及配置

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

  7. mysql 主从 配置和同步管理

    首先呢,需要有两个mysql服务器.如果做测试的话可以在同一台机器上装两个mysql服务程序,注意要两个运行程序的端口不能一样.我用的是一个是默认的3306,从服务器用的是3307端口. 在主服务创建 ...

  8. mysql主从复制(半同步方式)

    mysql主从复制(半同步方式) 博客分类: MySQL mysqlreplication复制  一.半同步复制原理介绍 1. 优点 当事务返回客户端成功后,则日志一定在至少两台主机上存在. MySQ ...

  9. mysql读写分离 主从同步

    MySQL主从复制与读写分离的实现 转载 2013年01月17日 18:20:12   MySQL主从复制与读写分离 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy) ...

随机推荐

  1. Memcached入门学习

    Memcached入门学习 学习网址:http://www.runoob.com/Memcached/Memcached-tutorial.html

  2. cmd命令查看当前IIS运行的网站iisapp appcmd

    xp,2003中用的命令是:iisapp -a 2007,2008,2012系统中: cmd命令行进入%windir%\system32\inetsrv\目录下 运行appcmd.exe list w ...

  3. java.lang.String & java.lang.StringBuilder

    java.lang.String & java.lang.StringBuilder String 成员方法 作用 public charAr(int index) 返回给定位置的代码单元 p ...

  4. Bitcoin Core钱包客户端的区块数据搬家指南

    最近在饭团(微信中的一个服务号)里教一些朋友学习比特币和区块链技术,为了让大家深刻地理解去中心化网络和钱包等概念,我推荐大家一定要安装经典的Bitcoin Core钱包软件,有些朋友在安装的时候没有留 ...

  5. HTTP 错误 500.21 - Internal Server Error 处理程序“WebServiceHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    解决方案是 解决方案:只需要重新安装一下就可以了.在Frameworv4.0的目录中安装的程序以管理员权限重新运行一下就可以了. %windir%\Microsoft.NET\Framework\v4 ...

  6. HanLP中人名识别分析

    HanLP中人名识别分析 在看源码之前,先看几遍论文<基于角色标注的中国人名自动识别研究> 关于命名识别的一些问题,可参考下列一些issue: 名字识别的问题 #387 机构名识别错误 关 ...

  7. springboot(二十):数据库连接池介绍

    概述 性能方面 hikariCP>druid>tomcat-jdbc>dbcp>c3p0 .hikariCP的高性能得益于最大限度的避免锁竞争. druid功能最为全面,sql ...

  8. C#一例绘制字体不清晰的解决办法

    public static Bitmap GetPieWithText(String text, Color color, Color fontColor,Font font) { ; Bitmap ...

  9. GCC编译器原理(二)------编译原理一:ELF文件(2)

    四. ELF 文件格式分析 ELF文件(目标文件)格式主要四种: 可重定向文件: 文件保存着代码和适当的数据,用来和其他的目标文件一起来创建一个可执行文件或者是一个共享目标文件.(目标文件或者静态库文 ...

  10. Git学习之连接GitHub远程仓库

    在看此教程之前电脑上应该已安装好git,并且配置好基本信息,Git新手请从头开始. 第1步:创建SSH Key 在用户主目录下(Mac系统是在用户主目录下,可通过命令ll -a查看,Windows下自 ...