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

实验目的:

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

方法:

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文件到空闲服务器:db212上
  • 以全备恢复到一个临时新实例.

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

方法三:利用binlog直接改名为realy-log直接回放SQL到指定点:

  • 恢复全备到临时实例中(略),并在关闭状态下。
root@localhost [(none)]>shutdown;
Query OK, 0 rows affected (0.00 sec)
[1]- Done /usr/local/mysql57/bin/mysqld --defaults-file=/data/57mysql/mysql3506/my3506.cnf
[root@db211_16:40:49 /data/57mysql/mysql3506/logs]
  • 复制备份binlog文件,将文件重命名为relay-bin.0000*格式,并制作relay-bin.index
#cd data/
[root@db211_16:40:58 /data/57mysql/mysql3506/data]
#ll
total 409652
-rw-r----- 1 mysql mysql 56 Aug 1 16:36 auto.cnf
-rw-r----- 1 mysql mysql 8585 Aug 1 16:40 error.log
-rw-r----- 1 mysql mysql 364 Aug 1 16:40 ib_buffer_pool
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:40 ibdata1
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:40 ib_logfile0
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:36 ib_logfile1
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:36 ib_logfile2
drwxr-x--- 2 mysql mysql 4096 Aug 1 16:36 mysql
drwxr-x--- 2 mysql mysql 8192 Aug 1 16:36 performance_schema
-rw-r----- 1 mysql mysql 380 Aug 1 16:38 slow.log
drwxr-x--- 2 mysql mysql 8192 Aug 1 16:36 sys
[root@db211_16:40:59 /data/57mysql/mysql3506/data]
#cp /data/backup/3506binlogs/* .
[root@db211_16:41:30 /data/57mysql/mysql3506/data]
#rename mysql-bin relay-bin mysql-bin.0000* //重命名成relay-bin.0000*
[root@db211_16:45:50 /data/57mysql/mysql3506/data]
#ll
total 3283784
-rw-r----- 1 mysql mysql 56 Aug 1 16:36 auto.cnf
-rw-r----- 1 mysql mysql 8585 Aug 1 16:40 error.log
-rw-r----- 1 mysql mysql 364 Aug 1 16:40 ib_buffer_pool
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:40 ibdata1
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:40 ib_logfile0
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:36 ib_logfile1
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:36 ib_logfile2
drwxr-x--- 2 mysql mysql 4096 Aug 1 16:36 mysql
drwxr-x--- 2 mysql mysql 8192 Aug 1 16:36 performance_schema
-rw-r----- 1 root root 268435555 Aug 1 16:41 relay-bin.000010
-rw-r----- 1 root root 268442595 Aug 1 16:41 relay-bin.000011
-rw-r----- 1 root root 268442419 Aug 1 16:41 relay-bin.000012
-rw-r----- 1 root root 268437857 Aug 1 16:41 relay-bin.000013
-rw-r----- 1 root root 268438414 Aug 1 16:41 relay-bin.000014
-rw-r----- 1 root root 268437131 Aug 1 16:41 relay-bin.000015
-rw-r----- 1 root root 268436889 Aug 1 16:41 relay-bin.000016
-rw-r----- 1 root root 268437011 Aug 1 16:41 relay-bin.000017
-rw-r----- 1 root root 268441823 Aug 1 16:41 relay-bin.000018
-rw-r----- 1 root root 268439288 Aug 1 16:41 relay-bin.000019
-rw-r----- 1 root root 258700685 Aug 1 16:41 relay-bin.000020
-rw-r----- 1 mysql mysql 380 Aug 1 16:38 slow.log
drwxr-x--- 2 mysql mysql 8192 Aug 1 16:36 sys
[root@db211_16:45:52 /data/57mysql/mysql3506/data]
#ls ./relay-bin.0000*>relay-bin.index //create relay-bin.index
[root@db211_16:46:24 /data/57mysql/mysql3506/data]
#cat relay-bin.index
./relay-bin.000010
./relay-bin.000011
./relay-bin.000012
./relay-bin.000013
./relay-bin.000014
./relay-bin.000015
./relay-bin.000016
./relay-bin.000017
./relay-bin.000018
./relay-bin.000019
./relay-bin.000020
[root@db211_16:46:31 /data/57mysql/mysql3506/data]
#chown mysql:mysql relay-bin.* //修改relay-bin权限为mysql:mysql
[root@db211_16:47:10 /data/57mysql/mysql3506/data]
#ll
total 3283788
-rw-r----- 1 mysql mysql 56 Aug 1 16:36 auto.cnf
-rw-r----- 1 mysql mysql 8585 Aug 1 16:40 error.log
-rw-r----- 1 mysql mysql 364 Aug 1 16:40 ib_buffer_pool
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:40 ibdata1
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:40 ib_logfile0
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:36 ib_logfile1
-rw-r----- 1 mysql mysql 104857600 Aug 1 16:36 ib_logfile2
drwxr-x--- 2 mysql mysql 4096 Aug 1 16:36 mysql
drwxr-x--- 2 mysql mysql 8192 Aug 1 16:36 performance_schema
-rw-r----- 1 mysql mysql 268435555 Aug 1 16:41 relay-bin.000010
-rw-r----- 1 mysql mysql 268442595 Aug 1 16:41 relay-bin.000011
-rw-r----- 1 mysql mysql 268442419 Aug 1 16:41 relay-bin.000012
-rw-r----- 1 mysql mysql 268437857 Aug 1 16:41 relay-bin.000013
-rw-r----- 1 mysql mysql 268438414 Aug 1 16:41 relay-bin.000014
-rw-r----- 1 mysql mysql 268437131 Aug 1 16:41 relay-bin.000015
-rw-r----- 1 mysql mysql 268436889 Aug 1 16:41 relay-bin.000016
-rw-r----- 1 mysql mysql 268437011 Aug 1 16:41 relay-bin.000017
-rw-r----- 1 mysql mysql 268441823 Aug 1 16:41 relay-bin.000018
-rw-r----- 1 mysql mysql 268439288 Aug 1 16:41 relay-bin.000019
-rw-r----- 1 mysql mysql 258700685 Aug 1 16:41 relay-bin.000020
-rw-r--r-- 1 mysql mysql 209 Aug 1 16:46 relay-bin.index
-rw-r----- 1 mysql mysql 380 Aug 1 16:38 slow.log
drwxr-x--- 2 mysql mysql 8192 Aug 1 16:36 sys
[root@db211_16:47:13 /data/57mysql/mysql3506/data]
  • 如果relay-bin的目录中有relay-log.info文件,一定得删掉该文件.

  • 启动mysqld 设置相关参数

[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)]>reset master;
root@localhost [(none)]>change master to relay_log_file='relay-bin.000014',relay_log_pos=251246776,master_host='xxx';
root@localhost [(none)]>start slave sql_thread until sql_before_gtids='9e912d2b-92f6-11e8-9747-0050568be82c:9791090';
  • 等待sql_thread 停在9791089时就可以查看恢复数据了.
root@localhost [(none)]>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: xxx
Master_User:
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: relay-bin.000020
Relay_Log_Pos: 97871478
Relay_Master_Log_File: mysql-bin.000020
Slave_IO_Running: No
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: 258700839
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: 0
Master_UUID:
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:
Executed_Gtid_Set: 9e912d2b-92f6-11e8-9747-0050568be82c:1-9791089
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec) ERROR:
No query specified root@localhost [(none)]>
root@localhost [(none)]>select count(*) from wenyz.t2;
+----------+
| count(*) |
+----------+
| 163818 |
+----------+
1 row in set (0.11 sec) root@localhost [(none)]>
  • 至此数据已经回来了.导入master的工作就不在重复,参照方法一中.

通过全备+relaylog同步恢复被drop的库或表的更多相关文章

  1. 通过全备+relaylog同步恢复被drop的库或表【转】

    MySQL 中drop 等高危误操作后恢复方法 实验目的: 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复. 方法: 利用master同步 伪master+Binlog+同步 利用b ...

  2. 通过全备+主从同步恢复被drop的库或表

    MySQL 中drop 等高危误操作后恢复方法 实验目的: 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复. 方法: 利用master同步(本文)] 伪master+Binlog+同 ...

  3. 通过全备+binlog_server同步恢复被drop的库或表

    MySQL 中drop 等高危误操作后恢复方法 实验目的: 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复. 方法: 利用master同步 :伪master+Binlog+同步(本文 ...

  4. mysql如何从全备文件中恢复单个库或者单个表

    mysql如何从全备文件中恢复单个库或者单个表 在mysql dba的日常实际工作中,一个实例下有多个库,而我们常见的备份就是全库备份.那么问题就来了,如果需要恢复单个库或者单个表,怎么办了,网上有很 ...

  5. MySQL没有备份怎么恢复被drop的表(利用undrop-for-innodb)

    介绍:     也许大家都难以理解,这么重要的数据为啥不备份(或者备份不可用)?而且还任性的drop table了.显然有备份是最好的,但是它们并不总是可用的.这种情况令人恐惧,但并非毫无希望.在许多 ...

  6. 没有备份怎么恢复被drop的表(利用undrop-for-innodb)

    介绍:     也许大家都难以理解,这么重要的数据为啥不备份(或者备份不可用)?而且还任性的drop table了.显然有备份是最好的,但是它们并不总是可用的.这种情况令人恐惧,但并非毫无希望.在许多 ...

  7. 使用全备+binlog日志恢复数据库

    1.binlog日志类型 Statement 只记录执行的sql语句,磁盘占用少,但是恢复的时候容易出问题.InodeDB不能使用Statement . Row 记录修改后的具体数据,磁盘占用较多 M ...

  8. 从MySQL全备文件中恢复单个库或者单个表

    从MySQL全备文件中恢复单个库或者单个表 提取建库语句 sed -n '/^-- Current Database: db_cms/,/^-- Current Database: `/p' back ...

  9. 如何恢复oracle中已删除的表

    在9i中Oracle引入了flashback的概念,可以将数据返回到某个时间点,但对于诸如drop/truncate等DDL语句却尚不支持.进入Oracle10g,这一缺陷得到了弥补.可以将丢失掉的表 ...

随机推荐

  1. 架构师成长之路6.2 DNS配置文件

    点击返回架构师成长之路 架构师成长之路6.2 DNS 配置文件 /etc/hosts 主机的一个文件列表 ,添加记录如:111.13.100.92   www.baidu.com 对于简单的主机名解析 ...

  2. 【BZOJ3309】DZY Loves Math(莫比乌斯反演)

    [BZOJ3309]DZY Loves Math(莫比乌斯反演) 题面 求 \[\sum_{i=1}^a\sum_{j=1}^bf(gcd(a,b))\] 其中,\(f(x)\)表示\(x\)分解质因 ...

  3. 【转】用宏定义代替printf函数

    问题提出 有时候我们想用宏定义来决定是编译debug版本的代码还是release的代码,dubug版本的代码会通过printf打印调试信息,release版本的代码则不会.我们总不能对每一条print ...

  4. wordcloud词云

    借鉴别人的一个小例子,快速生成词云的代码: from wordcloud import WordCloud f = open(u'txt/AliceEN.txt','r').read() wordcl ...

  5. php关于Session和cookie总结

    什么是 Cookie? cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,能够创建并取回 c ...

  6. Jenkins中配置邮件通知实例演示

    前言:本文通过安装配置Jenkins实现邮件通知,告知一个C# Git Repo的build成功与否 一.预配条件 在windows上安装Jenkins和它推荐安装的Plugins 创建一个@163. ...

  7. change username on ubuntu.

    Below tutorial will show you how to change username in ubuntu 12.04 precise.First,we need login as r ...

  8. 硬盘读取速度变慢 — 当前传送模式: PIO模式

    网上搜索了一下,找到两篇文章: 标题:硬盘读取速度变慢 当前传输模式pio的解决方法 http://www.veryhuo.com/a/view/52786.html   (解决思路:先卸载驱动,重启 ...

  9. CRC-16的原理和实现

    CRC的全称为Cyclic Redundancy Check,中文名称为循环冗余校验.它是一类重要的线性分组码,编码和解码方法简单,检错和纠错能力强,在通信领域广泛地用于实现差错控制.实际上,除 数据 ...

  10. 拦截器的使用,配置手机浏览器访问的h5页面

    package com.thinkgem.jeesite.modules.sys.interceptor; import javax.servlet.http.HttpServletRequest; ...