my.cnf中關于binlog的設置 binlog_format = row binlog格式必須是row模式 binlog_row_image = full 必須為full 默認就是full 準備工作 #yum intall git #yum install python-setuptools pip需要這個 #wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate # tar -xzvf pip-1.5.4.tar.gz # cd pip-1.5.4 # python setup.py install 安裝 # git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sql # pip install -r requirements.txt 用戶授權(用于binglog2sql使用) GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO xiaomage@'localhost' identified by 'xiaomage';
select:需要讀取server端information_schema.COLUMNS表,獲取表結構的元信息,拼接成可視化的sql語句super/replication client:兩個權限都可以,需要執行'SHOW MASTER STATUS', 獲取server端的binlog列表 replication slave:通過BINLOG_DUMP協議獲取binlog內容的權限
測試: mysql> select * from test;
+------+-------+----------+ | id | name | time | +------+-------+----------+ | 1 | lalal | 16:32:21 | | 2 | ababa | 16:32:34 | | 3 | jgjgj | 16:32:43 | | 4 | opopo | 16:32:52 | | 5 | jljlj | 16:33:06 | +------+-------+----------+ 5 rows in set (0.00 sec) mysql> delete from test; --清空表-- Query OK, 5 rows affected (0.01 sec) mysql> show master status; +------------------+----------+--------------+------------------+-------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------------------------------+ | mysql-bin.000013 | 2016 | | | f90ca153-b46d-11e6-ab68-0800279ecefa:1-35 | +------------------+----------+--------------+------------------+-------------------------------------------+ 一般情況下都是知道個大概的時間,先從大概的時間找起 [root@master binlog2sql]# python /opt/binlog2sql/binlog2sql/binlog2sql.py -uxiaomage -pxiaomage -dtest -ttest --start-file=mysql-bin.000013 --start-datetime='2017-05-05 16:30:00' --stop-datetime='2017-05-05 16:40:00' USE test; create table test (id int, name varchar(20),time time); INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:32:21', 1, 'lalal'); #start 478 end 659 time 2017-05-05 16:32:21 INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:32:34', 2, 'ababa'); #start 690 end 871 time 2017-05-05 16:32:34 INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:32:43', 3, 'jgjgj'); #start 902 end 1083 time 2017-05-05 16:32:43 INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:32:52', 4, 'opopo'); #start 1114 end 1295 time 2017-05-05 16:32:52 INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:33:06', 5, 'jljlj'); #start 1326 end 1507 time 2017-05-05 16:33:06 USE test; alter table test modify time datetime; USE test; alter table test modify time time; DELETE FROM `test`.`test` WHERE `time`='16:32:21' AND `id`=1 AND `name`='lalal' LIMIT 1; #start 1756 end 1985 time 2017-05-05 16:37:27 ----這就是delete的數據--- DELETE FROM `test`.`test` WHERE `time`='16:32:34' AND `id`=2 AND `name`='ababa' LIMIT 1; #start 1756 end 1985 time 2017-05-05 16:37:27 DELETE FROM `test`.`test` WHERE `time`='16:32:43' AND `id`=3 AND `name`='jgjgj' LIMIT 1; #start 1756 end 1985 time 2017-05-05 16:37:27 DELETE FROM `test`.`test` WHERE `time`='16:32:52' AND `id`=4 AND `name`='opopo' LIMIT 1; #start 1756 end 1985 time 2017-05-05 16:37:27 DELETE FROM `test`.`test` WHERE `time`='16:33:06' AND `id`=5 AND `name`='jljlj' LIMIT 1; #start 1756 end 1985 time 2017-05-05 16:37:27 生成回滾語句 -B,上面看到的具體的位置start 1756 end 1985(這里一定要過濾好了,一定要細心,配合grep什么的進行匹配) [root@master binlog2sql]# python /opt/binlog2sql/binlog2sql/binlog2sql.py -uxiaomage -pxiaomage -dtest -ttest --start-file=mysql-bin.000013 --start-position=1756 --stop-position=1985 -B INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:33:06', 5, 'jljlj'); #start 1756 end 1985 time 2017-05-05 16:37:27 INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:32:52', 4, 'opopo'); #start 1756 end 1985 time 2017-05-05 16:37:27 INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:32:43', 3, 'jgjgj'); #start 1756 end 1985 time 2017-05-05 16:37:27 INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:32:34', 2, 'ababa'); #start 1756 end 1985 time 2017-05-05 16:37:27 INSERT INTO `test`.`test`(`time`, `id`, `name`) VALUES ('16:32:21', 1, 'lalal'); #start 1756 end 1985 time 2017-05-05 16:37:27 重定向到/opt/rollback.sql文件中 恢復: mysql> select count(*) from test; +----------+ | count(*) | +----------+ | 0 | +----------+ 1 row in set (0.05 sec) mysql> source /opt/rollback.sql Query OK, 1 row affected (0.03 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.07 sec) Query OK, 1 row affected (0.04 sec) Query OK, 1 row affected (0.00 sec) mysql> select * from test; +------+-------+----------+ | id | name | time | +------+-------+----------+ | 5 | jljlj | 16:33:06 | | 4 | opopo | 16:32:52 | | 3 | jgjgj | 16:32:43 | | 2 | ababa | 16:32:34 | | 1 | lalal | 16:32:21 | +------+-------+----------+ 5 rows in set (0.00 sec) ok恢復會來了
解析模式 --stop-never 持續同步binlog??蛇x。不加則同步至執行命令時最新的binlog位置。 -K, --no-primary-key 對INSERT語句去除主鍵??蛇x。 -B, --flashback 生成回滾語句,可解析大文件,不受內存限制,每打印一千行加一句SLEEP SELECT(1)??蛇x。與stop-never或no-primary-key不能同時添加。
解析范圍控制 --start-file 起始解析文件。必須。 --start-position/--start-pos start-file的起始解析位置??蛇x。默認為start-file的起始位置。 --stop-file/--end-file 末尾解析文件??蛇x。默認為start-file同一個文件。 若解析模式為stop-never,此選項失效。 --stop-position/--end-pos stop-file的末尾解析位置??蛇x。默認為stop-file的最末位置;若解析模式為stop-never,此選項失效。 --start-datetime 從哪個時間點的binlog開始解析, 格式必須為datetime,如'2016-11-11 11:11:11'??蛇x。默認不過濾。 --stop-datetime 到哪個時間點的binlog停止解析,格式必須為datetime,如'2016-11-11 11:11:11'??蛇x。默認不過濾。
對象過濾 -d, --databases 只輸出目標db的sql??蛇x。默認為空。 -t, --tables 只輸出目標tables的sql??蛇x。默認為空
參考:https://github.com/danfengcao/binlog2sql
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。