mysql数据误删后的数据回滚
https://www.518cn.com 发布时间:2025-03-18 22:31 作者:网络
摘要:mysql数据误删后的数据回滚 第一步:下载MyFlash工具 # 创建文件夹 mkdir /back_data cd /back_data # 下载压缩包 wget https://codeload.github.com/Meituan-Dianping/MyFlash/zip/master #
mysql数据误删后的数据回滚
第一步:下载MyFlash工具
# 创建文件夹
mkdir /back_data
cd /back_data
# 下载压缩包
wget https://codeload.github.com/Meituan-Dianping/MyFlash/zip/master
# 安装编译相关软件
yum install gcc -y
yum install glib2 glib2-devel -y
# 解压缩包
yum -y install unzip
unzip master
# 进入软件目录
cd /back_data/MyFlash-master
# 编译
sh build.sh
#验证
cd binary
./flashback --help
如果显示
[root@localhost binary]# ll
总用量 7380
-rwxr-xr-x. 1 root root 87648 10月 31 14:20 flashback
-rwxr-xr-x. 1 root root 7463125 11月 5 2020 mysqlbinlog20160408
[root@localhost binary]# ./flashback --help
Usage:
flashback [OPTION?]
Help Options:
-h, --help Show help options
Application Options:
--databaseNames databaseName to apply. if multiple, seperate by comma(,)
--tableNames tableName to apply. if multiple, seperate by comma(,)
--tableNames-file tableName to apply. if multiple, seperate by comma(,)
--start-position start position
--stop-position stop position
--start-datetime start time (format %Y-%m-%d %H:%M:%S)
--stop-datetime stop time (format %Y-%m-%d %H:%M:%S)
--sqlTypes sql type to filter . support INSERT, UPDATE ,DELETE. if multiple, seperate by comma(,)
--maxSplitSize max file size after split, the uint is M
--binlogFileNames binlog files to process. if multiple, seperate by comma(,)
--outBinlogFileNameBase output binlog file name base
--logLevel log level, available option is debug,warning,error
--include-gtids gtids to process. if multiple, seperate by comma(,)
--include-gtids-file gtids to process. if multiple, seperate by comma(,)
--exclude-gtids gtids to skip. if multiple, seperate by comma(,)
--exclude-gtids-file gtids to skip. if multiple, seperate by comma(,)
[root@localhost binary]#
即安装成功!
第二步:开启binlog日志
#登录数据库 (一般或是mysql -uroot -p123456)
[root@localhost binary]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.6.40 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.
mysql>
#查询binlog日志是否开启
mysql> show variables%log_bin%';
--------------------------------- -------
| Variable_name | Value |
--------------------------------- -------
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
--------------------------------- -------
6 rows in set (0.01 sec)
mysql> SHOW VARIABLES LIKE '%binlog_row_image%';
------------------ -------
| Variable_name | Value |
------------------ -------
| binlog_row_image | FULL |
------------------ -------
1 row in set (0.00 sec)
mysql>
#开启binlog日志
#第一个:binlog_format=row
[root@localhost ~]# cd /etc/
[root@localhost etc]# vim my.cnf
[mysqld]
log-bin=mysql-bin
binlog_format=row
server_id=1
#第二个:binlog_row_image=FULL。
#默认开启
#重启mysqld
[root@localhost etc]# systemctl restart mysqld
第三步:检查第二步
[root@localhost etc]# systemctl restart mysqld
[root@localhost etc]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.40-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.
mysql> SHOW VARIABLES LIKE '%binlog_row_image%';
------------------ -------
| Variable_name | Value |
------------------ -------
| binlog_row_image | FULL |
------------------ -------
1 row in set (0.00 sec)
mysql> show variables like '%log_bin%';
--------------------------------- -----------------------------------------
| Variable_name | Value |
--------------------------------- -----------------------------------------
| log_bin | ON |
| log_bin_basename | /application/mysql/data/mysql-bin |
| log_bin_index | /application/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
--------------------------------- -----------------------------------------
6 rows in set (0.00 sec)
mysql>
第四步:开始测试
基础用法
# 查看所有binglog日志
SHOW MASTER LOGS;
# 当前使用的日志
show master status;
# 查看日志记录
show binlog events in '日志文件名';
建立测试表
create database if EXISTS itcast;
use itcast;
create table tb_user(
id int(11) not null,
name varchar(50) not null,
sex varchar(1),
primary key (id)
)engine=innodb default charset=utf8;
insert into tb_user(id,name,sex) values(1,'Tom','1');
insert into tb_user(id,name,sex) values(2,'Trigger','0');
insert into tb_user(id,name,sex) values(3,'Dawn','1');
第五步:误删数据库
查看删库后的binlog
第六步:新建binglog,减少外来日志影响
之后立即flush logs; 生成新的binlog
由于我们执行 flush logs 命令新生了一个文件,所以我们执行的删除的命令应该在 binlog.000001 文件里面
查看具体的命令行
show binlog events in 'mysql-bin.000001';
得到数据恢复的起始位置为 120,结束位置为 1166,
mysqlbinlog辅助恢复
接下来使用 mysqlbinlog 命令执行 binlog 文件,恢复数据,命令如下:
mysqlbinlog -v /application/mysql-5.6.40/data/mysql-bin.000001 --start-position=120 --stop-position=1166 | mysql -uroot -p123456
相关文章
- 优化GreatSQL日志文件空间占用 GreatSQL对于日志文件磁盘空间占用,做了一些优化,对于binlog、...03-18
- "数据约束条件" date: 2022-11-24T21:24:31 08:00 draft: false MySQL字段约束条件 无符号, 零填充...03-18
【GreatSQL优化器-16】INDEX_SKIP_SCAN
【GreatSQL优化器-16】INDEX_SKIP_SCAN 一、INDEX_SKIP_SCAN介绍 GreatSQL 优化器的索引跳跃扫描(Index Ski...03-18- MySQL 是一个非常流行的开源关系数据库管理系统,在各种应用场景中都得到了广泛的应用。随...03-18
- 🤖 DB-GPT 是一个开源的 AI 原生数据应用程序开发框架,具有 AWEL(代理工作流表达式语...03-18
GreatSQL 8.0.32-27 GA (2025-3-10)
GreatSQL 8.0.32-27 GA (2025-3-10) 版本信息 发布时间:2025年3月10日 版本号:8.0.32-27, Revision aa66a38591...03-18- 6. MySQL 索引的数据结构(详细说明) @目录6. MySQL 索引的数据结构(详细说明)1. 为什么使用索引2...03-18
- @Override @Transactional(rollbackFor = Exception.class) public void batchInsertDeviceData(IotMsgNotifyData iotMsgNotifyDa...03-18
- 个人Qt项目总结——数据库查询断言问题 问题: 当我使用MySQL数据库的查询操作时, 如果查询...03-18
- MySQL 是一种广泛使用的关系数据库管理系统,MySQL 8 是其最新的主要版本,结合了出色的性能和...03-18
最新评论