您的位置:首頁技術文章
文章詳情頁

MySQL恢復誤刪數據圖文教程

瀏覽:105日期:2023-09-05 20:31:47
目錄1、查看是否啟用 binlog 日志2、查看所有 binlog 日志3、查看正在使用的日志4、查找日志所在文件夾5、log 日志轉 sql6、delete 轉 insert 恢復誤刪總結

MySQL 恢復誤刪數據,針對 window 和 Linux 均適用,只需要找到對應的 binlog 目錄文件,默認就是 MySQL 安裝目錄下的 data 文件夾

一般誤刪數據,先停止所有操作,備份數據庫

# 備份所有數據庫mysqldump -uroot -p123456 --all-databases > /backup/mysqldump/all.db# 恢復數據mysql -uroot -p db_name < /backup/mysqldump/db_name.db1、查看是否啟用 binlog 日志SHOW VARIABLES LIKE '%log_bin%'

2、查看所有 binlog 日志SHOW BINARY LOGS;

3、查看正在使用的日志SHOW MASTER STATUS;

4、查找日志所在文件夾SHOW VARIABLES LIKE '%datadir%';

5、log 日志轉 sql

使用上面使用的 binlog.000056 文件,先把 該文件復制到其他地方

mysqlbinlog E:\test\result\binlog.000056 > E:\test\result\db.sql

有可能報錯

添加 --no-defaults 參數可以解決,但中文會亂碼(使用下面 vbs 或自定義語言實現可解決 亂碼問題),一般要加上時間字段轉換 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

轉換成功如下,可以看到刪除 sql 的語句

接下來只需要將上面的 delete 語句轉換為 inert 即可恢復誤刪數據,如果需要轉換的多可以通過代碼自定義實現,主要就是將delete 轉 insert

6、delete 轉 insert 恢復誤刪

通過 vbs 腳本轉換 sql 語句,當然也可以使用其他的語言,window 執行 vbs 不需要額外的環境,你只需要修改下面 vbs 文件中輸入輸出文件名以及編碼類型即可

'=========================='用VBS實現 MYSQL binglog DELETE 轉 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轉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轉成INSERT以后的新的SQL文件名newStream.Close

轉換結果如下,自行選擇新增恢復數據

總結

到此這篇關于MySQL恢復誤刪數據的文章就介紹到這了,更多相關MySQL恢復誤刪數據內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: MySQL 數據庫
国产综合久久一区二区三区