mysql binlog2sql_MySQL数据闪回 binlog2sql
相比于全備+binlog恢復(fù),flashback 是動(dòng)作更小的恢復(fù)方式。簡(jiǎn)單來說就是通過工具解析 binlog 生成與誤操作相反的SQL,比如 delete 則反向生成 insert,再執(zhí)行一遍即可恢復(fù)數(shù)據(jù)。第一個(gè)實(shí)現(xiàn)該功能的是彭立勛,他在 MySQL 5.5 版本上實(shí)現(xiàn),并將其提交給 MariaDB,Oracle MySQL 并沒有推出這個(gè)功能,由于跟版本綁定比較深,使用上不是很廣泛。美團(tuán)點(diǎn)評(píng)陸續(xù)也開發(fā)了兩款開源工具:binlog2mysql和MyFlash,比較受歡迎。
閃回的要求與限制:
要求binlog_format = row格式,且binlog_row_image = full;
只支持DML的閃回,不支持DDL。
目前支持版本
MySQL 5.6, 5.7
一、安裝工具
binlog2sql 基于python,支持2.7、3.4+版本,因此需要執(zhí)行該工具的環(huán)境已安裝 python 環(huán)境,然后再進(jìn)行軟件的安裝。
[root@centos ~]# yum -y install git python-pip
[root@centos ~]# git clone https://github.com/danfengcao/binlog2sql.git
[root@centos ~]# cd binlog2sql/
[root@centos binlog2sql]# pip install -r requirements.txt
二、模擬數(shù)據(jù)誤刪除
模擬 id 為 2、3 的兩行數(shù)據(jù)被誤刪除
執(zhí)行 flush logs; 是為了更加直觀的寫文檔,這一步可以忽略。
mysql> flush logs;
Query OK, 0 rows affected (0.04 sec)
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
+------+------+
3 rows in set (0.00 sec)
mysql> delete from t1 where id in (1,2);
Query OK, 2 rows affected (0.00 sec)
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 3 | cc |
+------+------+
1 row in set (0.00 sec)
查看當(dāng)前誤刪除時(shí) binlog 文件名( 一般不需要大概位置的 position 位點(diǎn)或 GTID 事務(wù)號(hào),因?yàn)榭梢允褂脮r(shí)間來選中日志范圍 )
mysql> show master status;
+------------------+----------+--------------+------------------+-----------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-----------------------------------------------+
| mysql-bin.000006 | 521 | | | 049f1f26-dd03-11ea-bc16-02000aba3c36:1-106260 |
+------------------+----------+--------------+------------------+-----------------------------------------------+
1 row in set (0.00 sec)
三、查看誤刪除時(shí)間執(zhí)行的DML語句
查看誤操作時(shí)間段內(nèi)執(zhí)行的DML語句( 假設(shè)誤操作在 14:00-14:20 )
每一個(gè) SQL 后都有 position 位點(diǎn)信息( start 259 end 490 )
[root@centos binlog2sql]# python ./binlog2sql/binlog2sql.py -h 10.186.60.54 -P 3306 -u root -p 123456 -d testdb -t t1 --start-file='mysql-bin.000006' --start-datetime='2020-08-13 14:00:00' --stop-datetime='2020-08-13 14:20:00'
DELETE FROM `testdb`.`t1` WHERE `id`=1 AND `name`='aa' LIMIT 1; #start 259 end 490 time 2020-08-13 14:17:39
DELETE FROM `testdb`.`t1` WHERE `id`=2 AND `name`='bb' LIMIT 1; #start 259 end 490 time 2020-08-13 14:17:39
四、生成回滾SQL語句
根據(jù)上一步獲取到的準(zhǔn)確 postion 位點(diǎn)信息,生成回滾語句。
[root@centos binlog2sql]# python ./binlog2sql/binlog2sql.py -h 10.186.60.54 -P 3306 -u root -p 123456 -d testdb -t t1 --flashback --start-file='mysql-bin.000006' --start-position='259' --stop-position='490' > /tmp/mysql_flashback_testdb_t1.sql
[root@centos binlog2sql]# cat /tmp/mysql_flashback_testdb_t1.sql
INSERT INTO `testdb`.`t1`(`id`, `name`) VALUES (2, 'bb'); #start 259 end 490 time 2020-08-13 14:17:39
INSERT INTO `testdb`.`t1`(`id`, `name`) VALUES (1, 'aa'); #start 259 end 490 time 2020-08-13 14:17:39
五、數(shù)據(jù)恢復(fù)
由業(yè)務(wù)方確認(rèn)待回滾的 SQL 語句內(nèi)容完全正確后,登錄MySQL客戶端執(zhí)行導(dǎo)入。
在數(shù)據(jù)恢復(fù)時(shí),要記得關(guān)閉 binlog 日志記錄,防止 binlog 日志被污染。
當(dāng)前測(cè)試 id 字段不是自增主鍵,所以閃回?cái)?shù)據(jù)后 id 字段數(shù)字為倒序的( 如果 id 字段是自增主鍵,那么會(huì)數(shù)據(jù)閃回后順序是遞增的,不會(huì)紊亂的。 )
mysql> use testdb;
Database changed
mysql> set sql_log_bin = off;
Query OK, 0 rows affected (0.00 sec)
mysql> source /tmp/mysql_flashback_testdb_t1.sql;
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
# 數(shù)據(jù)驗(yàn)證
mysql> select * from testdb.t1;
+------+------+
| id | name |
+------+------+
| 3 | cc |
| 2 | bb |
| 1 | aa |
+------+------+
3 rows in set (0.00 sec)
六、參數(shù)解釋
# python binlog2sql/binlog2sql.py --help
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 終止解析文件。可選。默認(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'??蛇x。默認(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)TRUE。
--sql-type 只解析指定類型,支持INSERT, UPDATE, DELETE。多個(gè)類型用空格隔開,如--sql-type INSERT DELETE。可選。默認(rèn)為增刪改都解析。用了此參數(shù)但沒填任何類型,則三者都不解析。
建議 MySQL 二進(jìn)制日志參數(shù)
# 在配置文件my.cnf的mysqld這個(gè)區(qū)下設(shè)置
[mysqld]
server_id = 1
log_bin = /data/mysql/log/binlog/3306/mysql-bin
max_binlog_size = 256M
binlog_format = row
binlog_row_image = full
# 在運(yùn)行中的mysql中查看
show variables like 'server_id';
show variables like 'log_bin%';
show variables like 'max_binlog_size';
show variables like 'binlog_format';
show variables like 'binlog_row_image';
用來閃回?cái)?shù)據(jù)的用戶需要的最小權(quán)限集合
# 建議授權(quán)
# select, super/replication client, replication slave
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@'localhost' identified by '123456';
# 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)限
總結(jié)
以上是生活随笔為你收集整理的mysql binlog2sql_MySQL数据闪回 binlog2sql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 住房公积金贷款条件 具有合法有效的身份证
- 下一篇: c 对一个mysql数据库进行操作_C/