前提

mysql必须有以下配置

binlog_format = rowbinlog_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地址:https://github.com/DHBin/ra

下载地址:https://github.com/DHBin/ra/tags

数据库工具反对binlog数据闪回、binlog转sql等等反对mysql数据库版本:5.5.x5.6.x5.7.x8.0.xUsage:  ra [command]Available Commands:  flashback   数据闪回  help        Help about any command  tosql       通过binlog日志生成sqlFlags:  -h, --help      help for ra  -v, --version   version for raUse "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 1682469713insert into `test`.`tb_user` (id, username, pwd, sex) values(2, '李四', '111111', '女'); # pos 5726 timestamp 1682469713insert into `test`.`tb_user` (id, username, pwd, sex) values(3, 'kk', '1111', '鸡'); # pos 5726 timestamp 1682469713

事件就是这样,小明不必跑路了,请小董喝了一瓶冰红茶。