window under Mysql recover deleted data Delete

Reprinted: https://www.cnblogs.com/q149072205/p/11940591.html

 

Navicat even mysql DB test the machine used and even the official DB, because the local official to the frequent operation so are a lot of open inquiry, was supposed to DELETE delete data test DB's, did not see the implementation of a formal environment. A total of 325 data is deleted, and then find ways to restore data on the Internet, it must be deleted DELETE, use the drop table if the table is no way to recover deleted, the specific recovery process is as follows

 

The first step: first check binlog function is turned on

 

show variables like '%log_bin%';

 

If log_bin can be restored to ON explanation , if it is not turned OFF Description binlog, there is no pre-generated rollback SQL, it may actually not be quickly rolled back, GAMEOVER, do not read the following (as if you could recover by ibd, concrete you can refer https://blog.csdn.net/hanjun0612/article/details/102466509).

 

 

   Step two: Check the data file exists side path

 

show variables like '%datadir%';

 

 

 Access to open the path where the database mysql-bin. **** this file, delete the note modification time DELETE time, compared to mysql-bin files, I was about to delete the data of No. 26 in the afternoon 18 points, so look for mysql -bin.000028 this file

 

 

 

  The third step: to find the mysql installation directory

 

show variables like "%basedir%";

 

 

 CMD command breaks were under Mysql installation directory, find mysqlbinlog.exe, did not say if you are installing a fake mysql.

 

 

 

  第四步.通过mysqlbinlog 恢复删除的数据日志记录

 

mysqlbinlog --base64-output=decode-rows -v --database=DBName --start-datetime="2019-11-26 18:00:00" --stop-datetime="2019-11-26 18:10:00" D:\MySQL\Data\mysql-bin_copy.000028 > mysqllog.sql

 

复制代码
mysqlbinlog 命令的参数说明
--base64-output=decode-rows //数据转换正常的字符,如果不设置这个参数将显示base64的数据
--database=DBName  //数据库名(一个mysql数据库比较多,指定方便恢复)
--start-datetime="2019-11-26 18:00:00"  //恢复起始时间
--stop-datetime="2019-11-26 18:10:00"  //恢复结束时间
D:\MySQL\Data\mysql-bin_copy.000028  //为数据恢复的日志文件
mysqllog.sql    //恢复以后我们需要的文件名
复制代码

 

 执行以后如下图:

 

 

 打开mysqllog.sql文件,搜索 DELETE 关键字,找到被删除数据,看到这些数据以后总算放心了,如下图:

 

 

  第五步:把mysqllog的DELETE转换为Insert语句,这个在liunx下操作方便(有人用python转换也可以),把文件mysqllog.sql复制Liunx下

 

cat mysqllog.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' > mysqllogOK.sql

 

 

 

 

 转换以后打开mysqllogOK.sql文件,因为批量替换前面多个一个分号,去掉以后,把所有生成的Insert语句在navicat下执行即可恢复所有DELETE删除的数据。

 

 

 总结:   

 

1.不要随便用DELETE,数据做用字段做删除标记,不用使用DELETE删除数据,如果非要删除,删除前一定先备份一下数据

 

2.数据库做好定时备份,如果数据库不大,可以一小时备份一次,再大也可以每天备份一次

 

3.做事细心,越熟练的事越要细心,本次DELETE导致的加班,就是以为自己对sql熟练就随意操作。

 

4.也是最重要的一点,一定要Mysql开启binlog功能

 

如果没开启binlog功能的赶紧开启一下吧,mysql开启binLog功能方法(windows),打开my.ini文件,添加如下配置,重启mysql即可开启

 

# log-bin
log-bin=mysql-bin 
binlog_format = ROW

 

 

Guess you like

Origin www.cnblogs.com/hanjun0612/p/11941434.html