条件

mysql必须有以下装备

binlog_format = row
binlog_row_image = full # 默许是full

实战

假设有一张用户表,结构如下

create table tb_user
(
    id       bigint primary key not null auto_increment,
    username varchar(100)       not null,
    pwd      varchar(100)       not null,
    sex      varchar(10)        not null
);

数据sql如下:

insert into tb_user (username, pwd, sex)
values ('张三', '123456', '男'),
       ('李四', '111111', '女'),
       ('kk', '1111', '鸡');

小明一天不小心履行了delete全表的操作

delete from tb_user where id != 0;

把数据全部删除了

生成回滚sql

小明都想好跑路的国家了,小董出手相助,祭出今天要介绍的东西ra,github地址:github.com/DHBin/ra

下载地址:github.com/DHBin/ra/ta…

数据库东西
支撑binlog数据闪回、binlog转sql等等
支撑mysql数据库版别:
5.5.x
5.6.x
5.7.x
8.0.x
Usage:
  ra [command]
Available Commands:
  flashback   数据闪回
  help        Help about any command
  tosql       经过binlog日志生成sql
Flags:
  -h, --help      help for ra
  -v, --version   version for ra
Use "ra [command] --help" for more information about a command.

过程一:检查当时的binlog文件名

show binary logs;
+----------------+---------+---------+
|Log_name        |File_size|Encrypted|
+----------------+---------+---------+
|mysql-bin.000010|7627     |No       |
|mysql-bin.000011|6699     |No       |
+----------------+---------+---------+

删除的binlog一般在最后的binlog文件中,mysql-bin.000011。依据小明的描绘,当时操作的时刻大概是2023-04-26 08:41

过程二:运用ra生成回滚sql

依据描绘得到两个关键的信息

  • binlog文件名
  • 操作时刻

把时刻规模圈在41分

ra flashback --host 127.0.0.1 -u root -p 123456  --start-datetime "2023-04-26 08:41:00" --stop-datetime "2023-04-26 08:42:00"

履行后生成回滚sql

insert into `test`.`tb_user` (id, username, pwd, sex) values(1, '张三', '123456', '男'); # pos 5726 timestamp 1682469713
insert into `test`.`tb_user` (id, username, pwd, sex) values(2, '李四', '111111', '女'); # pos 5726 timestamp 1682469713
insert into `test`.`tb_user` (id, username, pwd, sex) values(3, 'kk', '1111', '鸡'); # pos 5726 timestamp 1682469713

工作就是这样,小明不用跑路了,请小董喝了一瓶冰红茶。