(一)binlog2sql介紹html
binlog2sql是國內MySQL大佬danfengcao開發,許多MySQL愛好者參與改進的一款MySQL binlog解析軟件。根據不一樣選項,能夠獲得原始SQL、回滾SQL、去除主鍵的SQL等。python
github地址爲:https://github.com/danfengcao/binlog2sqlmysql
該工具主要用於:git
適用MySQL版本:MySQL5.6 、MySQL5.7github
(二)安裝binlog2sqlsql
# 安裝git shell> yum install -y git # 安裝pip工具 shell> yum install -y epel-release shell> yum install -y python-pip # 安裝binlog2sql shell> git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sql shell> pip install -r requirements.txt
(三)使用binlog2sqlshell
要使用binlog2sql,MySQL服務器須要設置如下參數:數據庫
[mysqld] server_id = 1 log_bin = /var/log/mysql/mysql-bin.log max_binlog_size = 100M binlog_format = row binlog_row_image = full
binlog2sql在使用時須要鏈接到數據庫上,鏈接用戶的權限爲:服務器
select,spuer/replication client,replication slave -- 建議受權: grant select,replication slave,replication client on *.* to user;
binlog2sql的語法爲:工具
[root@masterdb binlog2sql]# pwd /root/binlog2sql/binlog2sql [root@masterdb binlog2sql]# python binlog2sql.py --help usage: binlog2sql.py [-h HOST] [-u USER] [-p [PASSWORD [PASSWORD ...]]] [-P PORT] [--start-file START_FILE] [--start-position START_POS] [--stop-file END_FILE] [--stop-position END_POS] [--start-datetime START_TIME] [--stop-datetime STOP_TIME] [--stop-never] [--help] [-d [DATABASES [DATABASES ...]]] [-t [TABLES [TABLES ...]]] [--only-dml] [--sql-type [SQL_TYPE [SQL_TYPE ...]]] [-K] [-B] [--back-interval BACK_INTERVAL]
語法解析:
MySQL連接配置參數
解析模式參數:
範圍控制參數:
對象過濾參數:
(四)binlog2sql測試
測試目的:本次實驗模擬誤刪除數據,經過使用binlog2sql將數據找回來。
STEP1:構造測試數據
--構造測試表 create table test01 ( id int primary key, name varchar(30) not null, birthday date not null ); -插入3條數據 insert into test01 values(1,'小明','1993-01-02'); insert into test01 values(2,'小華','1994-08-15'); insert into test01 values(3,'小麗','1995-07-12'); mysql> select * from test01; +----+--------+------------+ | id | name | birthday | +----+--------+------------+ | 1 | 小明 | 1993-01-02 | | 2 | 小華 | 1994-08-15 | | 3 | 小麗 | 1995-07-12 | +----+--------+------------+ 3 rows in set (0.00 sec)
STEP2:模擬誤刪除數據
mysql> delete from test01; Query OK, 3 rows affected (0.00 sec)
STEP3:確認最後的日誌
mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000001 | 1600 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
STEP4:解析出標準SQL,用於定位回滾的開始和結束位置
[root@masterdb binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p123456 -dlijiamandb -t test01 --sql-type DELETE --start-file='master-bin.000001' USE lijiamandb; create table test01 ( id int primary key, name varchar(30) not null, birthday date not null ); DELETE FROM `lijiamandb`.`test01` WHERE `birthday`='1993-01-02' AND `id`=1 AND `name`='小明' LIMIT 1; #start 1287 end 1569 time 2020-04-24 13:40:26 DELETE FROM `lijiamandb`.`test01` WHERE `birthday`='1994-08-15' AND `id`=2 AND `name`='小華' LIMIT 1; #start 1287 end 1569 time 2020-04-24 13:40:26 DELETE FROM `lijiamandb`.`test01` WHERE `birthday`='1995-07-12' AND `id`=3 AND `name`='小麗' LIMIT 1; #start 1287 end 1569 time 2020-04-24 13:40:26
值得注意的是,雖然我指定了只解析DELETE語句,但仍是把DDL給解析出來了。
STEP5:解析出回滾SQL
[root@masterdb binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p123456 --flashback -dlijiamandb -t test01 --sql-type DELETE --start-file='master-bin.000001' INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1995-07-12', 3, '小麗'); #start 1287 end 1569 time 2020-04-24 13:40:26 INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1994-08-15', 2, '小華'); #start 1287 end 1569 time 2020-04-24 13:40:26 INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1993-01-02', 1, '小明'); #start 1287 end 1569 time 2020-04-24 13:40:26
STEP6:還原到數據庫
[root@masterdb binlog2sql]# mysql -uroot -p123456 lijiamandb mysql> INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1995-07-12', 3, '小麗'); #start 1287 end 1569 time 2020-04-24 13:40:26 69 time 2020-04-24 13:40:26 INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1993-01-02', 1, '小明'); #start 1287 end 1569 time 2020-04-24 13:40:26Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1994-08-15', 2, '小華'); #start 1287 end 1569 time 2020-04-24 13:40:26 Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1993-01-02', 1, '小明'); #start 1287 end 1569 time 2020-04-24 13:40:26 Query OK, 1 row affected (0.00 sec) mysql> select * from test01; +----+--------+------------+ | id | name | birthday | +----+--------+------------+ | 1 | 小明 | 1993-01-02 | | 2 | 小華 | 1994-08-15 | | 3 | 小麗 | 1995-07-12 | +----+--------+------------+ 3 rows in set (0.00 sec)
(五)總結
使用binlog2sql最大的好處就是解析出來的SQL語句很是直觀,而且在註釋中還包含了時間,這對於咱們去查找故障發生點很是實用。想想以前用過的mysqlbinlog工具,解析出來的結果中含有大量無關的信息,爲咱們排查問題增長了難度,而binlog2sql解析出來的SQL很是乾淨,便於咱們排查問題,恢復數據。
相關文檔集合: 1.MySQL日誌--二進制日誌(binlog) |