天天看点

MySQL - 40如何恢复误删的数据

什么是binlog日志

使用binlog恢复误删的数据

#查看所有二进制日志列表
show master logs;
#查看正在使用的二进制日志
show master status;
#刷新日志(重新开始新的binlog日志文件)
flush logs
#查询指定的binlog
show binlog events in 'binlog.000003' from 10668\G;
#导出恢复数据用的sql
mysqlbinlog "C:\ProgramData\MySQL\MySQL Server 8.0\Data\WQ-20160826MDKU-bin.000057" -- start-position 528 --stop-position 1191 > d:\backup\test.sql      

实战案例

创建表数据

create table test_binlog(
  id int not null auto_increment primary key,
  name varchar(50)
);

insert into test_binlog(name) values('aaa');
insert into test_binlog(name) values('bbb');
insert into test_binlog(name) values('ccc');
insert into test_binlog(name) values('ddd');

mysql> show master logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |   3104223 | No        |
| binlog.000002 |       200 | No        |
| binlog.000003 |     10747 | No        |
+---------------+-----------+-----------+
3 rows in set (0.00 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000003 |    10747 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)

mysql> delete from test_binlog where id in(2, 3);
Query OK, 2 rows affected (0.01 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000005 |      156 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)      

查看所有二进制日志列表

mysql> show master logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |   3104223 | No        |
| binlog.000002 |       200 | No        |
| binlog.000003 |     10747 | No        |
+---------------+-----------+-----------+
3 rows in set (0.00 sec)
mysql>      

查看正在使用的二进制日志

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000003 |    10747 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>      

查询指定的binlog

mysql> show binlog events in 'binlog.000003'\G;
*************************** 1. row ***************************
   Log_name: binlog.000003
        Pos: 4
 Event_type: Format_desc
  Server_id: 1
End_log_pos: 125
       Info: Server ver: 8.0.20, Binlog ver: 4      

导出恢复数据用的sql

root@9abe1dd59947:~# mysqlbinlog -v --base64-output=decode-rows "/var/lib/mysql/binlog.000003"  

root@9abe1dd59947:~# mysqlbinlog "/var/lib/mysql/binlog.000003" --start-position 9834 --stop-position 10716 > ~/test.sql

mysql> source /root/test.sql

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

...

mysql> select * from test_binlog;
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | bbb  |
|  3 | ccc  |
|  4 | ddd  |
+----+------+
4 rows in set (0.01 sec)

mysql>