mysql5.7.44误删除数据后,使用binlog日志恢复

系统环境

bash 复制代码
#系统版本
# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
#数据库版本
# mysql -V
mysql  Ver 14.14 Distrib 5.7.44, for Linux (x86_64) using  EditLine wrapper

恢复前提

本文仅适合mysql数据库已经开启binlog日志的情况。
查看binlog是否开启

bash 复制代码
mysql> show variables like '%log_bin%';
+---------------------------------+--------------------------------+
| Variable_name                   | Value                          |
+---------------------------------+--------------------------------+
| log_bin                         | ON                             |
| log_bin_basename                | /var/lib/mysql/mysql-bin       |
| log_bin_index                   | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF                            |
| log_bin_use_v1_row_events       | OFF                            |
| sql_log_bin                     | ON                             |
+---------------------------------+--------------------------------+
# ON 表示成功开启

查看binlog路径

bash 复制代码
mysql> show variables like '%datadir%';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| datadir       | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)

恢复操作

模拟删除数据

bash 复制代码
mysql> delete from task_fail where id = 20;
Query OK, 1 row affected (0.38 sec)

mysql> delete from task_fail where id = 21;
Query OK, 1 row affected (0.04 sec)

mysql> delete from task_fail where id = 22;
Query OK, 1 row affected (0.06 sec)

mysql> delete from task_fail where id = 23;
Query OK, 1 row affected (0.07 sec)

mysql> select id from task_fail order by id asc;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 24 |
| 25 |
| 26 |

将删除时间段binlog翻译为sql

bash 复制代码
# mysqlbinlog --base64-output=decode-rows -v --database=airflow --start-datetime="2024-05-07 15:30:00" --stop-datetime="2024-05-07 15:50:00" "/var/lib/mysql/mysql-bin.000001" > delete.sql

将上述的sql文件delete修改为insert的sql

bash 复制代码
# cat delete.sql | sed -n '/###/p' | sed 's/### //g;s/\/\*.*/,/g;s/DELETE FROM/;INSERT INTO/g;s/WHERE/SELECT/g;' |sed -r 's/(@17.*),/\1;/g' | sed 's/@1=//g'| sed 's/@[1-9]=/,/g' | sed 's/@[1-9][0-9]=/,/g' > insert.sql

执行insert sql,恢复误删除数据

bash 复制代码
mysql> source /root/insert.sql
ERROR:
No query specified

Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

验收数据恢复情况

bash 复制代码
mysql> select id from task_fail order by id asc;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |

id 20 21 22 23 的数据已经恢复。

相关推荐
Sam_Deep_Thinking12 分钟前
在 MySQL 里,不建议使用长事务的根因
mysql
5***r93517 分钟前
开源数据同步中间件(Dbsyncer)简单玩一下 mysql to mysql 的增量,全量配置
mysql·中间件·开源
艾莉丝努力练剑1 小时前
【Git:多人协作】Git多人协作实战:从同分支到多分支工作流
服务器·c++·人工智能·git·gitee·centos·项目管理
2301_789380495 小时前
vsc中使用DBHub-MCP连接云Mysql到vsc-copilot
数据库·mysql
杨DaB5 小时前
【MySQL】02 数据库的基本操作
数据库·mysql·oracle
w***48827 小时前
Linux安装redis
linux·运维·redis
wanhengidc9 小时前
深度了解云手机是什么
运维·服务器·科技·智能手机·云计算
python百炼成钢9 小时前
28.嵌入式 Linux LED 驱动开发实验
linux·运维·驱动开发
Y***K4349 小时前
MySQL网站
数据库·mysql
菜鸟‍9 小时前
【后端学习】MySQL数据库
数据库·后端·学习·mysql