MySQL閃回工具之binlog2sql

一、binlog2sql

1.1 安裝binlog2sql

git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sql
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

binlog2sql參數

# python binlog2sql/binlog2sql.py --help
選項
mysql連接配置
-h host; -P port; -u user; -p password
解析模式
--stop-never 持續(xù)解析binlog??蛇x。,默認False,同步至執(zhí)行命令時最新的binlog位置。
-K, --no-primary-key 對INSERT語句去除主鍵??蛇x。默認False
-B, --flashback 生成回滾SQL,可解析大文件,不受內存限制??蛇x。默認False。與stop-never或no-primary-key不能同時添加。
--back-interval -B模式下,每打印一千行回滾SQL,加一句SLEEP多少秒,如不想加SLEEP,請設為0??蛇x。默認1.0。
解析范圍控制
--start-file 起始解析文件,只需文件名,無需全路徑 。必須。
--start-position/--start-pos 起始解析位置??蛇x。默認為start-file的起始位置。
--stop-file/--end-file 終止解析文件??蛇x。默認為start-file同一個文件。若解析模式為stop-never,此選項失效。
--stop-position/--end-pos 終止解析位置。可選。默認為stop-file的最末位置;若解析模式為stop-never,此選項失效。
--start-datetime 起始解析時間,格式'%Y-%m-%d %H:%M:%S'。可選。默認不過濾。
--stop-datetime 終止解析時間,格式'%Y-%m-%d %H:%M:%S'??蛇x。默認不過濾。
對象過濾
-d, --databases 只解析目標db的sql,多個庫用空格隔開,如-d db1 db2。可選。默認為空。
-t, --tables 只解析目標table的sql,多張表用空格隔開,如-t tbl1 tbl2??蛇x。默認為空。
--only-dml 只解析dml,忽略ddl??蛇x。默認TRUE。
--sql-type 只解析指定類型,支持INSERT, UPDATE, DELETE。多個類型用空格隔開,如--sql-type INSERT DELETE??蛇x。默認為增刪改都解析。用了此參數但沒填任何類型,則三者都不解析。

MySQL server參數

# 在配置文件my.cnf的mysqld這個區(qū)下設置
[mysqld]
server_id = 1
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 1G
binlog_format = row
binlog_row_image = full

# 在運行中的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';

用來閃回數據的user需要的最小權限集合

# 建議授權
# select, super/replication client, replication slave
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@'localhost' identified by '123456';

# select:需要讀取server端information_schema.COLUMNS表,獲取表結構的元信息,拼接成可視化的sql語句
# super/replication client:兩個權限都可以,需要執(zhí)行'SHOW MASTER STATUS', 獲取server端的binlog列表
# replication slave:通過BINLOG_DUMP協(xié)議獲取binlog內容的權限

二、應用案例

2.1 誤刪整張表數據,需要緊急回滾

# 數據庫
mysql> show create database cms\G
*************************** 1. row ***************************
       Database: cms
Create Database: CREATE DATABASE `cms` /*!40100 DEFAULT CHARACTER SET utf8 */
1 row in set (0.00 sec)

# 表結構
mysql> show create table t2\G
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `code` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_code` (`code`),
  KEY `idx_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

# 插入測試數據
insert into t2 (name, code) values ('a', '101'), ('b', '102'), ('c', '103');

# 數據
mysql> select * from t2;
+----+------+------+
| id | name | code |
+----+------+------+
| 20 | a    | 101  |
| 21 | b    | 102  |
| 22 | c    | 103  |
+----+------+------+
3 rows in set (0.00 sec)

# 刷新日志,實際不需要,這里只是為了讓重新生成一個日志
flush logs;

# 刪除表數據
mysql> delete from t2;
Query OK, 5 rows affected (0.00 sec)

mysql> select * from t2;
Empty set (0.00 sec)

查看binlog文件

# show binary logs;
mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000026
         Position: 459
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: db6406b5-1b06-11e8-8dc3-525400ad3b43:1-47
1 row in set (0.00 sec)

根據操作時間定位誤操作SQL的binlog位置

# cd ~/binlog2sql
python binlog2sql/binlog2sql.py \
-hlocalhost -P3306 -urepl -p'123456' \
--start-file='mysql-bin.000026' \
-dcms -tt2 \
--start-datetime='2018-03-15 19:00:00' \
--stop-datetime='2018-03-15 19:09:00'
image

從結果找至誤操作的位置為239-428之間,生成回滾sql

python binlog2sql/binlog2sql.py \
-hlocalhost -P3306 -urepl -p'123456' \
--start-file='mysql-bin.000026' \
-dcms -tt2 \
--start-position=239 \
--stop-position=428 \
-B > rollback.sql

查看回滾sql

image

確認回滾sql正確,執(zhí)行回滾sql

mysql < rollback.sql

三、總結

3.1 限制(對比mysqlbinlog)

  • mysql server必須開啟,離線模式下不能解析
  • 參數 binlog_row_image 必須為FULL,暫不支持MINIMAL
  • 解析速度不如mysqlbinlog

3.2 優(yōu)點(對比mysqlbinlog)

  • 純Python開發(fā),安裝與使用都很簡單
  • 自帶flashback、no-primary-key解析模式,無需再裝補丁
  • flashback模式下,更適合閃回實戰(zhàn)
  • 解析為標準SQL,方便理解、篩選
  • 代碼容易改造,可以支持更多個性化解析

四、參考

binlog2sql項目地址

MyFlash——美團點評的開源MySQL閃回工具

MySQL下實現(xiàn)閃回的設計思路 (MySQL Flashback Feature)

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容