MySQL如何恢复误删的数据?

本节目标

1.了解binlog日志
2.掌握如何恢复误删除的数据【重点】

什么是binlog日志

在这里插入图片描述

binlog日志的作用

1.在企业应用中,我们不是单台节点运行的,不会在一台服务器上装mysql来跑,
玩万一挂了怎么办,所以我们基于负载均衡这个概念,做到主从,一主多从的复制。
2.万一不小心把数据库给删了,我们可以通过binlog日志进行数据的恢复
但是要满足两个条件
一,定时全备份,例如每天凌晨一点定时备份
二,binlog日志是开启的

常用命令

该命令可以查看binlog是否开启及所在位置
show variables like ‘%log_bin%’;

查看所有二进制日志列表
show master logs;

查看正在使用的二进制日志
show master status;

刷新日志(重新开始新的binlog日志文件)
flush logs

查询指定的binlog
show binlog events in ‘WQ-20160826MDKU-bin.000050’ from 10668\G;

使用binlog恢复误删的数据

导出恢复数据用的sql,从哪里到哪里
mysqlbinlog ‘路径’ --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');

show master logs;

mysql> show master logs;
+----------------------------+-----------+-----------+
| Log_name                   | File_size | Encrypted |
+----------------------------+-----------+-----------+
| DESKTOP-VOA4M8V-bin.000001 |       178 | No        |
| DESKTOP-VOA4M8V-bin.000002 |     12188 | No        |
| DESKTOP-VOA4M8V-bin.000003 |      4196 | No        |
| DESKTOP-VOA4M8V-bin.000004 |      6286 | No        |
| DESKTOP-VOA4M8V-bin.000005 |      8424 | No        |
| DESKTOP-VOA4M8V-bin.000006 |       178 | No        |
| DESKTOP-VOA4M8V-bin.000007 |      2066 | No        |
| DESKTOP-VOA4M8V-bin.000008 | 254452865 | No        |
| DESKTOP-VOA4M8V-bin.000009 |      1941 | No        |
| DESKTOP-VOA4M8V-bin.000010 |       178 | No        |
| DESKTOP-VOA4M8V-bin.000011 |      1910 | No        |
| DESKTOP-VOA4M8V-bin.000012 |      1562 | No        |
| DESKTOP-VOA4M8V-bin.000013 |      6619 | No        |
| DESKTOP-VOA4M8V-bin.000014 |      2397 | No        |
| DESKTOP-VOA4M8V-bin.000015 |     31180 | No        |
| DESKTOP-VOA4M8V-bin.000016 |       178 | No        |
| DESKTOP-VOA4M8V-bin.000017 |      2492 | No        |
+----------------------------+-----------+-----------+
17 rows in set (0.62 sec)

查看正在使用的二进制日志
show master status;

mysql> show master status;
+----------------------------+----------+--------------+------------------+-------------------+
| File                       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------------+----------+--------------+------------------+-------------------+
| DESKTOP-VOA4M8V-bin.000017 |     2492 |              |                  |                   |
+----------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.04 sec)

结束当前日志,开启一个新的日志
flush logs;

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


mysql> show master status;
+----------------------------+----------+--------------+------------------+-------------------+
| File                       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------------+----------+--------------+------------------+-------------------+
| DESKTOP-VOA4M8V-bin.000018 |      155 |              |                  |                   |
+----------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

假装删除其中两条数据

delete from test_binlog where id in (2,3);

再次刷新flush logs;然后查看最新日志文件名字

mysql> show master status;
+----------------------------+----------+--------------+------------------+-------------------+
| File                       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------------+----------+--------------+------------------+-------------------+
| DESKTOP-VOA4M8V-bin.000019 |      155 |              |                  |                   |
+----------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

我们是在 DESKTOP-VOA4M8V-bin.000017下面进行数据的插入,
在 DESKTOP-VOA4M8V-bin.000018进行了数据的删除

开始恢复数据
show binlog events in ‘DESKTOP-VOA4M8V-bin.000017’\G;
或者
在dos窗口下,注意,不是在数据库下面
mysqlbinlog -v --base64-output=decode-rows “C:\ProgramData\MySQL\MySQL Server 8.0\Data\DESKTOP-VOA4M8V-bin.000017”
查看输出信息可知
从begin

at 1726


COMMIT/!/;

at 2198

恢复,在dos窗口下面执行
mysqlbinlog “C:\ProgramData\MySQL\MySQL Server 8.0\Data\DESKTOP-VOA4M8V-bin.000017” --start-position 1726 --stop-position 2198 > d:\backup\test.sql

最后source一下,数据恢复成功
source d:\backup\test.sql

mysql> select * from test_binlog;
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | bbb  |
|  3 | ccc  |
|  4 | ddd  |
+----+------+
4 rows in set (0.00 sec)
发布了82 篇原创文章 · 获赞 19 · 访问量 4632

猜你喜欢

转载自blog.csdn.net/ABCisCOOL/article/details/105526642