MySQL數(shù)據(jù)誤刪除的快速解決方法(MySQL閃回工具)
概述
Binlog2sql是一個(gè)Python開發(fā)開源的MySQL Binlog解析工具,能夠?qū)inlog解析為原始的SQL,也支持將Binlog解析為回滾的SQL,去除主鍵的INSERT SQL,是DBA和運(yùn)維人員數(shù)據(jù)恢復(fù)好幫手。
一、安裝配置
1.1 用途
數(shù)據(jù)快速回滾(閃回)
主從切換后新master丟數(shù)據(jù)的修復(fù)
從binlog生成標(biāo)準(zhǔn)SQL,帶來的衍生功能
支持MySQL5.6,5.7
1.2 安裝
shell> git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sql
shell> pip install -r requirements.txt
二、使用方法
2.1 使用前配置
2.1.1參數(shù)配置
[mysqld] server_id = 1 log_bin = /var/log/mysql/mysql-bin.log max_binlog_size = 1G binlog_format = row binlog_row_image = full
2.1.2 user需要的最小權(quán)限集合
select, super/replication client, replication slave
建議授權(quán)
select, super/replication client, replication slave
權(quán)限說明
- select:需要讀取server端information_schema.COLUMNS表,獲取表結(jié)構(gòu)的元信息,拼接成可視化的sql語句
- super/replication client:兩個(gè)權(quán)限都可以,需要執(zhí)行'SHOW MASTER STATUS', 獲取server端的binlog列表
- replication slave:通過BINLOG_DUMP協(xié)議獲取binlog內(nèi)容的權(quán)限
2.2 基本用法
2.2.1基本用法
解析出標(biāo)準(zhǔn)SQL
shell> python binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -t test3 test4 --start-file='mysql-bin.000002'
輸出:
INSERT INTO `test`.`test3`(`addtime`, `data`, `id`) VALUES ('2016-12-10 13:03:38', 'english', 4); #start 570 end 736
UPDATE `test`.`test3` SET `addtime`='2016-12-10 12:00:00', `data`='中文', `id`=3 WHERE `addtime`='2016-12-10 13:03:22' AND `data`='中文' AND `id`=3 LIMIT 1; #start 763 end 954
DELETE FROM `test`.`test3` WHERE `addtime`='2016-12-10 13:03:38' AND `data`='english' AND `id`=4 LIMIT 1; #start 981 end 1147
解析出回滾SQL
shell> python binlog2sql.py --flashback -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttest3 --start-file='mysql-bin.000002' --start-position=763 --stop-position=1147
輸出:
INSERT INTO `test`.`test3`(`addtime`, `data`, `id`) VALUES ('2016-12-10 13:03:38', 'english', 4); #start 981 end 1147
UPDATE `test`.`test3` SET `addtime`='2016-12-10 13:03:22', `data`='中文', `id`=3 WHERE `addtime`='2016-12-10 12:00:00' AND `data`='中文' AND `id`=3 LIMIT 1; #start 763 end 954
2.2.2 選項(xiàng)
mysql連接配置
-h host; -P port; -u user; -p password
解析模式
--stop-never 持續(xù)解析binlog??蛇x。默認(rèn)False,同步至執(zhí)行命令時(shí)最新的binlog位置。
-K, --no-primary-key 對(duì)INSERT語句去除主鍵??蛇x。默認(rèn)False
-B, --flashback 生成回滾SQL,可解析大文件,不受內(nèi)存限制??蛇x。默認(rèn)False。與stop-never或no-primary-key不能同時(shí)添加。
--back-interval -B模式下,每打印一千行回滾SQL,加一句SLEEP多少秒,如不想加SLEEP,請(qǐng)?jiān)O(shè)為0??蛇x。默認(rèn)1.0。
解析范圍控制
--start-file 起始解析文件,只需文件名,無需全路徑 。必須。
--start-position/--start-pos 起始解析位置。可選。默認(rèn)為start-file的起始位置。
--stop-file/--end-file 終止解析文件??蛇x。默認(rèn)為start-file同一個(gè)文件。若解析模式為stop-never,此選項(xiàng)失效。
--stop-position/--end-pos 終止解析位置??蛇x。默認(rèn)為stop-file的最末位置;若解析模式為stop-never,此選項(xiàng)失效。
--start-datetime 起始解析時(shí)間,格式'%Y-%m-%d %H:%M:%S'。可選。默認(rèn)不過濾。
--stop-datetime 終止解析時(shí)間,格式'%Y-%m-%d %H:%M:%S'??蛇x。默認(rèn)不過濾。
對(duì)象過濾
-d, --databases 只解析目標(biāo)db的sql,多個(gè)庫用空格隔開,如-d db1 db2??蛇x。默認(rèn)為空。
-t, --tables 只解析目標(biāo)table的sql,多張表用空格隔開,如-t tbl1 tbl2??蛇x。默認(rèn)為空。
--only-dml 只解析dml,忽略ddl??蛇x。默認(rèn)False。
--sql-type 只解析指定類型,支持INSERT, UPDATE, DELETE。多個(gè)類型用空格隔開,如--sql-type INSERT DELETE。可選。默認(rèn)為增刪改都解析。用了此參數(shù)但沒填任何類型,則三者都不解析。
2.3 應(yīng)用案例
2.3.1 誤刪整張表數(shù)據(jù),需要緊急回滾
閃回詳細(xì)介紹可參見example目錄下《閃回原理與實(shí)戰(zhàn)》example/mysql-flashback-priciple-and-practice.md
test庫tbl表原有數(shù)據(jù)
mysql> select * from tbl; +----+--------+---------------------+ | id | name | addtime | +----+--------+---------------------+ | 1 | 小趙 | 2016-12-10 00:04:33 | | 2 | 小錢 | 2016-12-10 00:04:48 | | 3 | 小孫 | 2016-12-13 20:25:00 | | 4 | 小李 | 2016-12-12 00:00:00 | +----+--------+---------------------+ 4 rows in set (0.00 sec) mysql> delete from tbl; Query OK, 4 rows affected (0.00 sec) 20:28時(shí),tbl表誤操作被清空 mysql> select * from tbl; Empty set (0.00 sec)
恢復(fù)數(shù)據(jù)步驟:
1、登錄mysql,查看目前的binlog文件
mysql> show master status; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000051 | 967 | | mysql-bin.000052 | 965 | +------------------+-----------+
2、最新的binlog文件是mysql-bin.000052,我們?cè)俣ㄎ徽`操作SQL的binlog位置。誤操作人只能知道大致的誤操作時(shí)間,我們根據(jù)大致時(shí)間過濾數(shù)據(jù)。
shell> python binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttbl --start-file='mysql-bin.000052' --start-datetime='2016-12-13 20:25:00' --stop-datetime='2016-12-13 20:30:00'
輸出:
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-13 20:26:00', 4, '小李'); #start 317 end 487 time 2016-12-13 20:26:26
UPDATE `test`.`tbl` SET `addtime`='2016-12-12 00:00:00', `id`=4, `name`='小李' WHERE `addtime`='2016-12-13 20:26:00' AND `id`=4 AND `name`='小李' LIMIT 1; #start 514 end 701 time 2016-12-13 20:27:07
DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-10 00:04:33' AND `id`=1 AND `name`='小趙' LIMIT 1; #start 728 end 938 time 2016-12-13 20:28:05
DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-10 00:04:48' AND `id`=2 AND `name`='小錢' LIMIT 1; #start 728 end 938 time 2016-12-13 20:28:05
DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-13 20:25:00' AND `id`=3 AND `name`='小孫' LIMIT 1; #start 728 end 938 time 2016-12-13 20:28:05
DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-12 00:00:00' AND `id`=4 AND `name`='小李' LIMIT 1; #start 728 end 938 time 2016-12-13 20:28:05
3、我們得到了誤操作sql的準(zhǔn)確位置在728-938之間,再根據(jù)位置進(jìn)一步過濾,使用flashback模式生成回滾sql,檢查回滾sql是否正確(注:真實(shí)環(huán)境下,此步經(jīng)常會(huì)進(jìn)一步篩選出需要的sql。結(jié)合grep、編輯器等)
shell> python binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttbl --start-file='mysql-bin.000052' --start-position=3346 --stop-position=3556 -B > rollback.sql | cat
輸出:
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-12 00:00:00', 4, '小李'); #start 728 end 938 time 2016-12-13 20:28:05
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-13 20:25:00', 3, '小孫'); #start 728 end 938 time 2016-12-13 20:28:05
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-10 00:04:48', 2, '小錢'); #start 728 end 938 time 2016-12-13 20:28:05
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-10 00:04:33', 1, '小趙'); #start 728 end 938 time 2016-12-13 20:28:05
4、確認(rèn)回滾sql正確,執(zhí)行回滾語句。登錄mysql確認(rèn),數(shù)據(jù)回滾成功。
shell> mysql -h127.0.0.1 -P3306 -uadmin -p'admin' < rollback.sql mysql> select * from tbl; +----+--------+---------------------+ | id | name | addtime | +----+--------+---------------------+ | 1 | 小趙 | 2016-12-10 00:04:33 | | 2 | 小錢 | 2016-12-10 00:04:48 | | 3 | 小孫 | 2016-12-13 20:25:00 | | 4 | 小李 | 2016-12-12 00:00:00 | +----+--------+---------------------+
三、總結(jié)
3.1 限制(對(duì)比mysqlbinlog)
mysql server必須開啟,離線模式下不能解析
參數(shù) binlog_row_image 必須為FULL,暫不支持MINIMAL
解析速度不如mysqlbinlog
3.2 優(yōu)點(diǎn)(對(duì)比mysqlbinlog)
純Python開發(fā),安裝與使用都很簡(jiǎn)單
自帶flashback、no-primary-key解析模式,無需再裝補(bǔ)丁
flashback模式下,更適合閃回實(shí)戰(zhàn)
解析為標(biāo)準(zhǔn)SQL,方便理解、篩選
代碼容易改造,可以支持更多個(gè)性化解析
總結(jié)
以上所述是小編給大家介紹的MySQL數(shù)據(jù)誤刪除的快速解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- mysql 找回誤刪表的數(shù)據(jù)方法(必看)
- 關(guān)于mysql數(shù)據(jù)庫誤刪除后的數(shù)據(jù)恢復(fù)操作說明
- MySQL數(shù)據(jù)庫誤刪恢復(fù)的超詳細(xì)教程
- Mysql恢復(fù)誤刪庫表數(shù)據(jù)完整場(chǎng)景演示
- MySQL誤刪后使用binlog恢復(fù)數(shù)據(jù)的實(shí)現(xiàn)方法
- MySQL數(shù)據(jù)庫誤刪回滾的解決
- MYSQL?Binlog恢復(fù)誤刪數(shù)據(jù)庫詳解
- MySQL恢復(fù)誤刪數(shù)據(jù)圖文教程
- MySQL數(shù)據(jù)被誤刪的解決方法
- MySQL數(shù)據(jù)庫誤刪數(shù)據(jù)該怎么解決(這里有救!)
相關(guān)文章
MYSQL安裝時(shí)解決要輸入current root password的解決方法
在裝MYSQL的時(shí)候發(fā)現(xiàn)要輸入current root password不記得以前在電腦里裝過(你的系統(tǒng)曾經(jīng)裝過MYSQL在重裝就會(huì)要求輸入原來設(shè)定的密碼,如果是第一次安裝就不會(huì)出現(xiàn)),在網(wǎng)上苦苦搜尋解決方法。2011-07-07
MySQL實(shí)現(xiàn)類似于connect_by_isleaf的功能MySQL方法或存儲(chǔ)過程
這篇文章主要介紹了MySQL實(shí)現(xiàn)類似于connect_by_isleaf的功能MySQL方法或存儲(chǔ)過程,需要的朋友可以參考下2017-02-02
mysql使用xtrbackup+relaylog增量恢復(fù)注意事項(xiàng)
這篇文章主要介紹了mysql使用xtrbackup+relaylog增量恢復(fù),本次實(shí)驗(yàn)mysql5.7.19.使用了GTID,row格式的binlog,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
獲取MySQL數(shù)據(jù)表列信息的三種方法實(shí)現(xiàn)
本文介紹了獲取MySQL數(shù)據(jù)表列信息的三種方法實(shí)現(xiàn),包含SHOWCOLUMNS命令、DESCRIBE命令以及查詢INFORMATION_SCHEMA.COLUMNS表,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
MySql存儲(chǔ)過程和游標(biāo)的使用實(shí)例
我們?cè)趯?shí)際的開發(fā)中會(huì)遇到一些統(tǒng)計(jì)的業(yè)務(wù)功能,如果我實(shí)時(shí)的去查詢的話有時(shí)候會(huì)很慢,此時(shí)我們可以寫一個(gè)存儲(chǔ)過程來實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于MySql存儲(chǔ)過程和游標(biāo)使用的相關(guān)資料,需要的朋友可以參考下2022-04-04

