0


【MYSQL】误删数据恢复流程说明

误删数据恢复流程说明

项目场景:使用navicat时,不小心误删除几条数据,需要对数据进行恢复


解决方案:

第一步:保证mysql已经开启binlog,查看命令

如果没有开启binlog,也没有预先生成回滚SQL,那可能真的无法快速回滚了。对存放重要业务数据的MySQL,强烈建议开启binlog。

查看binklog是否开启

show variables like'%log_bin%';

查看binlog存放日志文件目录(博主binlog目录为/home/mysql/binlog):

show variables like'%datadir%';

在这里插入图片描述

第二步:找到mysqlbinlog
在这里插入图片描述
第三步:找到删除时间段的binlog,翻译为sql

mysqlbinlog --base64-output=decode-rows -v --database=数据库名 --start-datetime="2023-01-31 11:43:37"--stop-datetime="2023-01-31 11:43:38"/home/mysql/binlog/mysql-bin.000325 > /home/backup/test1234.sql

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

function replaceregex(patern,str,tagstr)
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(“test1234.sql”) 'binLog生成的DELETE原日志文件
oldText = oldStream.ReadText()
newText=replace(oldText,“### DELETE FROM”, “;INSERT INTO”)
newText=replace(newText,“### WHERE”, “SELECT”)
newText=replace(newText,“###”, “”)
newText=replace(newText,“@1=”, “”)
newText=replaceregex(“@[1-9]=”,newText, “,”)
newText=replaceregex(“@[1-9][0-9]=”,newText, “,”)
oldStream.Close
'VBS保存文件
Set newStream = CreateObject(“ADODB.Stream”)
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

第五步:打开生成的mysqllogOK.sql文件,找到insert语句并执行

因为我这里是一条一条删除的,所以需要对数据重新编辑一下
#原sql 从上到下为:主键id,姓名,手机号,身份证,创建时间INSERTINTO`order_passenger`SELECT20034,张三
    ,"18866669999","11010119900307635X","2023-01-31 10:09:35"#修改后sqlINSERTINTO`order_passenger`VALUES(20034,张三
    ,"18866669999","11010119900307635X","2023-01-31 10:09:35")
标签: mysql 数据库

本文转载自: https://blog.csdn.net/qq_43374968/article/details/128815916
版权归原作者 西米C 所有, 如有侵权,请联系我们删除。

“【MYSQL】误删数据恢复流程说明”的评论:

还没有评论