MySQL恢復(fù)誤刪數(shù)據(jù)圖文教程
MySQL 恢復(fù)誤刪數(shù)據(jù),針對 window 和 Linux 均適用,只需要找到對應(yīng)的 binlog 目錄文件,默認(rèn)就是 MySQL 安裝目錄下的 data 文件夾
一般誤刪數(shù)據(jù),先停止所有操作,備份數(shù)據(jù)庫
# 備份所有數(shù)據(jù)庫mysqldump -uroot -p123456 --all-databases > /backup/mysqldump/all.db# 恢復(fù)數(shù)據(jù)mysql -uroot -p db_name < /backup/mysqldump/db_name.db1、查看是否啟用 binlog 日志SHOW VARIABLES LIKE '%log_bin%'使用上面使用的 binlog.000056 文件,先把 該文件復(fù)制到其他地方
mysqlbinlog E:\test\result\binlog.000056 > E:\test\result\db.sql有可能報錯
添加 --no-defaults 參數(shù)可以解決,但中文會亂碼(使用下面 vbs 或自定義語言實(shí)現(xiàn)可解決 亂碼問題),一般要加上時間字段轉(zhuǎn)換 sql
mysqlbinlog --no-defaults --base64-output=decode-rows -v --database=oauth --start-datetime='2023-06-01 01:44:00' --stop-datetime='2023-06-01 01:48:00' F:\mysql-8.0.29-winx64\data\binlog.000058 > E:\test\result\db.sql轉(zhuǎn)換成功如下,可以看到刪除 sql 的語句
接下來只需要將上面的 delete 語句轉(zhuǎn)換為 inert 即可恢復(fù)誤刪數(shù)據(jù),如果需要轉(zhuǎn)換的多可以通過代碼自定義實(shí)現(xiàn),主要就是將delete 轉(zhuǎn) insert
6、delete 轉(zhuǎn) insert 恢復(fù)誤刪通過 vbs 腳本轉(zhuǎn)換 sql 語句,當(dāng)然也可以使用其他的語言,window 執(zhí)行 vbs 不需要額外的環(huán)境,你只需要修改下面 vbs 文件中輸入輸出文件名以及編碼類型即可
'=========================='用VBS實(shí)現(xiàn) MYSQL binglog DELETE 轉(zhuǎn) INSERT'==========================function replaceregex(patern,str,tagstr)dim regex,matchesset regex=new regExpregex.pattern=paternregex.IgnoreCase=trueregex.global=truematches=regex.replace(str,tagstr)replaceregex=matchesend function'Mysql binlog DELETE轉(zhuǎn)INSERT=========='VBS打開文本文件Set oldStream = CreateObject('ADODB.Stream')oldStream.CharSet = 'utf-8'oldStream.Open'binLog 生成的 DELETE 原日志文件'oldStream.LoadFromFile('E:\test\result\db.sql') 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 objectnewStream.WriteText newTextnewStream.SaveToFile 'mysqllogOK.sql', 2 'DELETE轉(zhuǎn)成INSERT以后的新的SQL文件名newStream.Close轉(zhuǎn)換結(jié)果如下,自行選擇新增恢復(fù)數(shù)據(jù)
到此這篇關(guān)于MySQL恢復(fù)誤刪數(shù)據(jù)的文章就介紹到這了,更多相關(guān)MySQL恢復(fù)誤刪數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
