MySQL數據庫誤刪回滾的解決
某次一不小心,用了delete from xxx 刪除了幾條重要數據,在網上找了很多方法,但都比較零散,打算記錄本次數據找回的過程。
大致分為以下幾步
1、查看binlog是否開啟
# log_bin是ON,就說明打開了 OFF就是關閉狀態(tài),以下操作,只有為 ON 時有效。 show variables like 'log_bin';
2、找到binlog文件名
show master logs;
運行以上代碼,如下圖 TS1-bin.000009 就是我們要找的文件名
3、查看binlog日志位置
show variables like '%datadir%';
4、根據上面得到的位置,去找到 TS1-bin.000009 文件
5、進入到mysql安裝目錄的bin目錄下,執(zhí)行以下命令根據誤刪除的時間范圍從TS1-bin.000009文件導出成sql文件
mysqlbinlog --base64-output=decode-rows -v --database=數據庫名 --start-datetime="2022-06-29 15:35:00" --stop-datetime="2022-06-29 15:45:00" C:/Users/Administrator/Desktop/TS1-bin.000009 > C:/Users/Administrator/Desktop/mysqllog.sql
這里我把 TS1-bin.000009 文件拷貝到了桌面,因為該文件原始存放路徑有空格,導致命令執(zhí)行失敗,無法找到路徑。
得到 mysqllog.sql 文件后,可以用記事本打開,搜索 DELETE 關鍵字,找到刪除數據的記錄
6、將 DELETE 語句改造成 INSERT 語句,在windows下用vbs來實現,把下面代碼復制保存為:deleteToinsert.vbs 文件(一定要是.vbs格式文件) 與mysqllog.sql在同一目錄下,然后雙擊運行,會生成mysqllogOK.sql文件就是我們要的INSERT語句
'==========================? '用VBS實現 MYSQL binglog DELETE轉INSERT? '==========================? 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("mysqllog.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
7、拿到對應的 INSERT 語句后執(zhí)行。
參考文章
https://blog.csdn.net/qq_36602951/article/details/120729047
https://juejin.cn/post/7028955574242902023
到此這篇關于MySQL數據庫誤刪回滾的解決的文章就介紹到這了,更多相關MySQL數據庫誤刪回滾內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談sql連接查詢的區(qū)別 inner,left,right,full
下面小編就為大家?guī)硪黄獪\談sql連接查詢的區(qū)別 inner,left,right,full。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10