首頁  >  文章  >  資料庫  >  MySQL資料庫誤刪回滾怎麼解決

MySQL資料庫誤刪回滾怎麼解決

WBOY
WBOY轉載
2023-05-31 17:40:061000瀏覽

某次一不小心,用了delete from xxx 刪除了幾條重要數據,在網上找了很多方法,但都比較零散,打算記錄本次數據找回的過程。
大致分為以下幾步

1、查看binlog是否開啟

# log_bin是ON,就说明打开了 OFF就是关闭状态,以下操作,只有为 ON 时有效。
show variables like 'log_bin';

2、找到binlog檔名

show master logs;

執行以上程式碼,如下圖TS1-bin .000009 就是我們要找的檔名

MySQL資料庫誤刪回滾怎麼解決

3、查看binlog日誌位置

show variables like '%datadir%';

4、根據上面得到的位置,去找到TS1-bin .000009 檔案

#5、進入到mysql安裝目錄的bin目錄下,執行以下指令根據誤刪除的時間範圍從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 檔案拷貝到了桌面,因為該檔案原始存放路徑有空格,導致指令執行失敗,無法找到路徑。
得到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 語句後執行。

以上是MySQL資料庫誤刪回滾怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除