MySQL数据库误删回滚怎么解决
這篇文章主要講解了“MySQL數據庫誤刪回滾怎么解決”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“MySQL數據庫誤刪回滾怎么解決”吧!
某次一不小心,用了delete from xxx 刪除了幾條重要數據,在網上找了很多方法,但都比較零散,打算記錄本次數據找回的過程。
大致分為以下幾步
1、查看binlog是否開啟
#log_bin是ON,就說明打開了OFF就是關閉狀態,以下操作,只有為ON時有效。 showvariableslike'log_bin';
2、找到binlog文件名
showmasterlogs;
運行以上代碼,如下圖 TS1-bin.000009 就是我們要找的文件名
3、查看binlog日志位置
showvariableslike'%datadir%';
4、根據上面得到的位置,去找到 TS1-bin.000009 文件
5、進入到mysql安裝目錄的bin目錄下,執行以下命令根據誤刪除的時間范圍從TS1-bin.000009文件導出成sql文件
mysqlbinlog--base64-output=decode-rows-v--database=數據庫名--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 文件拷貝到了桌面,因為該文件原始存放路徑有空格,導致命令執行失敗,無法找到路徑。
得到 mysqllog.sql 文件后,可以用記事本打開,搜索 DELETE 關鍵字,找到刪除數據的記錄
6、將 DELETE 語句改造成 INSERT 語句,在windows下用vbs來實現,把下面代碼復制保存為:deleteToinsert.vbs 文件(一定要是.vbs格式文件) 與mysqllog.sql在同一目錄下,然后雙擊運行,會生成mysqllogOK.sql文件就是我們要的INSERT語句
'==========================
'用VBS實現MYSQLbinglogDELETE轉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轉INSERT================
'VBS打開文本文件
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轉成INSERT以后的新的SQL文件名
newStream.Close
7、拿到對應的 INSERT 語句后執行。
總結
以上是生活随笔為你收集整理的MySQL数据库误删回滚怎么解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZooKeeper学习第七期--ZooK
- 下一篇: 在Excel2010里面怎么隐藏行和列单