开发者

Mysql误删除DELETE数据找回操作指南

开发者 https://www.devze.com 2023-11-19 11:16 出处:网络 作者: 千硕
1.查看binlog是否开启 show variables like \'%log_bin%\'; 2.查看数据文件存放路径:bin-log路径

1.查看binlog是否开启

show variables like '%log_bin%';

Mysql误删除DELETE数据找回操作指南

2.查看数据文件存放路径:bin-log路径

show variables like '%datadir%';

Mysql误删除DELETE数据找回操作指南

3.找到删除时间段的binlog,翻译为sql

mysqlbinlog --base64-output=decode-rows -v --database=数据库名 --start-datetime="2022-10-19 16:40:00" --stop-datetime="2022-10-19 16:450:00" "D:\Program Files\mysql-5.7.30-winx64\mysqldb\bin.000041" > d:\mysqllog.sql

4.在生成的 mysqllog.sql 文件同路径下 ,生成将delete翻译为insert的sql文件

linux:

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

5、Windows:

在生成的sql文件同目录下,创建.vbs文件,写入如下内容,并双击执行

'==========================
'用vbs实现 MYSQL binglog DELETE转INSERT
'=======================RtmrWHpfsX===
function replaceregex(patern,str,www.devze.comtagstr)
dim regex,matches
set regex=new regExp
regex.pattern=patern
regex.IgnoreCase=true
regex.global=true
matches=regex.replace(str,tagstr)
replaceregex=matches
end function

'Mysql binlog DELETE转INSERT==========
'VBS打开文本文件
Set oldStream = CreateObject(“ADODB.Stream”)
oldStream.CharSet = “utf-8”
oldStream.Open
oldStream.LoadFromFile(“mysqllog.sql”) 'binLog生成的DELETE原日志文件
oldText = oldStream.ReadText()
newText=replace(oldTextpython,“### DELETE FROM”, “;INSERT INTO”)
newText=replace(newText,“### WHERE”, “SELECT”)
newText=replace(newText,js“###”, “”)
newText=replace(newText,“@1=”, “”)
newText=replaceregex(“@[1-9]=”,newText, “,”)
newText=replaceregex(“@[1-9][0-9]=”,newText, “,”)
oldStream.Close
'VBS保存文件
Set newStream = CreateObject(“ADODB.Strhttp://www.devze.comeam”)
newStream.Type = 2 'Specify stream type - we want To save text/string data.
newStream.Charset = “utf-8” 'Specify charset For the source text data.
newStream.Open 'Open the stream And write binary data To the object
newStream.WriteText newText
newStream.SaveToFile “mysqllogOK.sql”, 2 'DELETE转成INSERT以后的新的SQL文件名
newStream.Close

6、把mysqllog.sql文件和生成的windows中的.vbs放在同一个文件夹中双击执行生成mysqllogOK.sql文件

7、进入mysqllogOK.sql文件删除其中的delete保留insert再去执行mysqllogOK.sql就成功了

8、如果binlog日志没有开启可以进行开启

linux:

cd /usr/etc/vim my.cnf

Mysql误删除DELETE数据找回操作指南

log-bin=mysql-bin # 开启Binlog 一般只需要修改这一行即可
binlog-format=ROW # 设置格式 此行可以不加 命令设置即可 详见下方拓展
server_id=1 # 配置serverID 这一行本来就存在
systemctl restart mysqld #重启mysql

总结

到此这篇关于Mysql误删除DELETE数据找回操作的文章就介绍到这了,更多相关Mysql误删除数据找回内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号