MySQL数据库误删回滚怎么解决
這篇文章主要講解了“MySQL數(shù)據(jù)庫(kù)誤刪回滾怎么解決”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“MySQL數(shù)據(jù)庫(kù)誤刪回滾怎么解決”吧!
某次一不小心,用了delete from xxx 刪除了幾條重要數(shù)據(jù),在網(wǎng)上找了很多方法,但都比較零散,打算記錄本次數(shù)據(jù)找回的過(guò)程。
大致分為以下幾步
1、查看binlog是否開(kāi)啟
#log_bin是ON,就說(shuō)明打開(kāi)了OFF就是關(guān)閉狀態(tài),以下操作,只有為ON時(shí)有效。 showvariableslike'log_bin';
2、找到binlog文件名
showmasterlogs;
運(yùn)行以上代碼,如下圖 TS1-bin.000009 就是我們要找的文件名
3、查看binlog日志位置
showvariableslike'%datadir%';
4、根據(jù)上面得到的位置,去找到 TS1-bin.000009 文件
5、進(jìn)入到mysql安裝目錄的bin目錄下,執(zhí)行以下命令根據(jù)誤刪除的時(shí)間范圍從TS1-bin.000009文件導(dǎo)出成sql文件
mysqlbinlog--base64-output=decode-rows-v--database=數(shù)據(jù)庫(kù)名--start-datetime="2022-06-2915:35:00"--stop-datetime="2022-06-2915:45:00"C:/Users/Administrator/Desktop/TS1-bin.000009>C:/Users/Administrator/Desktop/mysqllog.sql
這里我把 TS1-bin.000009 文件拷貝到了桌面,因?yàn)樵撐募即娣怕窂接锌崭瘢瑢?dǎo)致命令執(zhí)行失敗,無(wú)法找到路徑。
得到 mysqllog.sql 文件后,可以用記事本打開(kāi),搜索 DELETE 關(guān)鍵字,找到刪除數(shù)據(jù)的記錄
6、將 DELETE 語(yǔ)句改造成 INSERT 語(yǔ)句,在windows下用vbs來(lái)實(shí)現(xiàn),把下面代碼復(fù)制保存為:deleteToinsert.vbs 文件(一定要是.vbs格式文件) 與mysqllog.sql在同一目錄下,然后雙擊運(yùn)行,會(huì)生成mysqllogOK.sql文件就是我們要的INSERT語(yǔ)句
'==========================
'用VBS實(shí)現(xiàn)MYSQLbinglogDELETE轉(zhuǎn)INSERT
'==========================
functionreplaceregex(patern,str,tagstr)
dimregex,matches
setregex=newregExp
regex.pattern=patern
regex.IgnoreCase=true
regex.global=true
matches=regex.replace(str,tagstr)
replaceregex=matches
endfunction
'======MysqlbinlogDELETE轉(zhuǎn)INSERT================
'VBS打開(kāi)文本文件
SetoldStream=CreateObject("ADODB.Stream")
oldStream.CharSet="utf-8"
oldStream.Open
oldStream.LoadFromFile("mysqllog.sql")'binLog生成的DELETE原日志文件
oldText=oldStream.ReadText()
newText=replace(oldText,"###DELETEFROM",";INSERTINTO")
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保存文件
SetnewStream=CreateObject("ADODB.Stream")
newStream.Type=2'Specifystreamtype-wewantTosavetext/stringdata.
newStream.Charset="utf-8"'SpecifycharsetForthesourcetextdata.
newStream.Open'OpenthestreamAndwritebinarydataTotheobject
newStream.WriteTextnewText
newStream.SaveToFile"mysqllogOK.sql",2'DELETE轉(zhuǎn)成INSERT以后的新的SQL文件名
newStream.Close
7、拿到對(duì)應(yīng)的 INSERT 語(yǔ)句后執(zhí)行。
總結(jié)
以上是生活随笔為你收集整理的MySQL数据库误删回滚怎么解决的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ZooKeeper学习第七期--ZooK
- 下一篇: 在Excel2010里面怎么隐藏行和列单