一扭弧、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參數(shù)
# python binlog2sql/binlog2sql.py --help
選項
mysql連接配置
-h host; -P port; -u user; -p password
解析模式
--stop-never 持續(xù)解析binlog膀息∩倭海可選。鲫忍,默認False逐抑,同步至執(zhí)行命令時最新的binlog位置鸠儿。
-K, --no-primary-key 對INSERT語句去除主鍵”靡蓿可選捆交。默認False
-B, --flashback 生成回滾SQL淑翼,可解析大文件,不受內(nèi)存限制品追⌒ǎ可選。默認False肉瓦。與stop-never或no-primary-key不能同時添加遭京。
--back-interval -B模式下,每打印一千行回滾SQL泞莉,加一句SLEEP多少秒哪雕,如不想加SLEEP,請設(shè)為0鲫趁∷购浚可選。默認1.0挨厚。
解析范圍控制
--start-file 起始解析文件堡僻,只需文件名,無需全路徑 疫剃。必須钉疫。
--start-position/--start-pos 起始解析位置〕布郏可選牲阁。默認為start-file的起始位置。
--stop-file/--end-file 終止解析文件壤躲〕蔷眨可選。默認為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'诚隙⊙纫可選。默認不過濾久又。
對象過濾
-d, --databases 只解析目標(biāo)db的sql巫延,多個庫用空格隔開,如-d db1 db2地消÷澹可選。默認為空脉执。
-t, --tables 只解析目標(biāo)table的sql疼阔,多張表用空格隔開,如-t tbl1 tbl2半夷「涂可選。默認為空玻熙。
--only-dml 只解析dml否彩,忽略ddl∴滤妫可選列荔。默認TRUE。
--sql-type 只解析指定類型枚尼,支持INSERT, UPDATE, DELETE贴浙。多個類型用空格隔開,如--sql-type INSERT DELETE署恍∑槔#可選。默認為增刪改都解析盯质。用了此參數(shù)但沒填任何類型袁串,則三者都不解析。
MySQL server參數(shù)
# 在配置文件my.cnf的mysqld這個區(qū)下設(shè)置
[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';
用來閃回數(shù)據(jù)的user需要的最小權(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:兩個權(quán)限都可以,需要執(zhí)行'SHOW MASTER STATUS', 獲取server端的binlog列表
# replication slave:通過BINLOG_DUMP協(xié)議獲取binlog內(nèi)容的權(quán)限
二王悍、應(yīng)用案例
2.1 誤刪整張表數(shù)據(jù)破镰,需要緊急回滾
# 數(shù)據(jù)庫
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)
# 表結(jié)構(gòu)
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)
# 插入測試數(shù)據(jù)
insert into t2 (name, code) values ('a', '101'), ('b', '102'), ('c', '103');
# 數(shù)據(jù)
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;
# 刪除表數(shù)據(jù)
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)
根據(jù)操作時間定位誤操作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
從結(jié)果找至誤操作的位置為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
三、總結(jié)
3.1 限制(對比mysqlbinlog)
- mysql server必須開啟孕似,離線模式下不能解析
- 參數(shù) binlog_row_image 必須為FULL瓶佳,暫不支持MINIMAL
- 解析速度不如mysqlbinlog
3.2 優(yōu)點(對比mysqlbinlog)
- 純Python開發(fā),安裝與使用都很簡單
- 自帶flashback鳞青、no-primary-key解析模式臂拓,無需再裝補丁
- flashback模式下傻工,更適合閃回實戰(zhàn)
- 解析為標(biāo)準(zhǔn)SQL,方便理解泄伪、篩選
- 代碼容易改造,可以支持更多個性化解析