您好,登錄后才能下訂單哦!
1. 背景
* 為了數據安全,搭建了主從。實時主從備份只能防止硬件問題,比如主庫的硬盤損壞。但對于誤操作,則無能為力。比如在主庫誤刪一張表,或者一個update語句沒有指定where條件,導致全表被更新。當操作被同步到從庫上后,則主從都“回天無力”。
* 閃回用于快速恢復由于誤操作丟失的數據。在DBA誤操作時,可以把數據庫恢復到以前某個時間點(或者說某個binlog的某個pos)。比如忘了帶where條件的update、delete操作,傳統的恢復方式是利用全備+二進制日志前滾進行恢復,相比于傳統的全備+增備,flashback顯然更為快速、簡單。
2. 測試環境
mysql> show variables like 'version'; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.6.36-log | +---------------+------------+ 1 row in set (0.03 sec) mysql> show variables like 'datadir'; +---------------+--------------------+ | Variable_name | Value | +---------------+--------------------+ | datadir | /data/mysql_data6/ | +---------------+--------------------+ 1 row in set (0.00 sec) mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0.00 sec) mysql> show variables like 'binlog_format'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | ROW | +---------------+-------+ 1 row in set (0.00 sec) mysql> show variables like 'binlog_row_p_w_picpath'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | binlog_row_p_w_picpath | FULL | +------------------+-------+ 1 row in set (0.00 sec)
3. 閃回實戰
* 創建數據庫與測試表,并插入數據
mysql> create table users( -> id BIGINT NOT NULL AUTO_INCREMENT, -> name VARCHAR(255) NOT NULL, -> sex ENUM('M', 'F') NOT NULL DEFAULT 'M', -> age INT UNSIGNED NOT NULL DEFAULT '0', -> PRIMARY KEY (id) -> )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected (0.04 sec) mysql> insert into users values(null, 'tom', 'M', 25), (null, 'jak', 'F', 32), (null, 'ses', 'M', 45), (null, 'lisea', 'M', 35); Query OK, 4 rows affected (0.13 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from users; +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 25 | | 2 | jak | F | 32 | | 3 | ses | M | 45 | | 4 | lisea | M | 35 | +----+-------+-----+-----+ 4 rows in set (0.00 sec)
* 下載閃回工具binlog2sql[ 由上海美團DBA團隊出品 ]
[root@MySQL ~]# git clone https://github.com/danfengcao/binlog2sql.git Initialized empty Git repository in /root/binlog2sql/.git/ remote: Counting objects: 244, done. remote: Total 244 (delta 0), reused 0 (delta 0), pack-reused 244 Receiving objects: 100% (244/244), 121.72 KiB | 27 KiB/s, done. Resolving deltas: 100% (124/124), done.
* 安裝相關依賴
[root@MySQL ~]# yum install pip -y [root@MySQL ~]# pip install --upgrade pip [root@MySQL ~]# pip install -r binlog2sql/requirements.txt
* 提前刷新binlog [ 測試中好區分文件 ]
mysql> flush logs; Query OK, 0 rows affected (0.02 sec)
* 查看當前binlog信息
mysql> show master status; +------------+----------+--------------+------------------+-------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------+----------+--------------+------------------+-------------------------------------------+ | bin.000006 | 191 | | | c7f82640-6b2d-11e7-9316-000c29f0b169:1-22 | +------------+----------+--------------+------------------+-------------------------------------------+ 1 row in set (0.01 sec)
* 誤操作,delete沒帶where條件
mysql> delete from users; Query OK, 4 rows affected (0.01 sec)
* binlog2sql工具通過文件輸出操作信息,定位SQL開始位置與結束位置
可通過--start-datetime與--stop-datetime定位時間
由此得到開始position為239,結束position為483
[root@MySQL ~]# python binlog2sql/binlog2sql/binlog2sql.py -hlocalhost -P3306 -uroot -p'123' -dmytest -tusers --start-file='bin.000006' DELETE FROM `mytest`.`users` WHERE `age`=25 AND `sex`='M' AND `id`=1 AND `name`='tom' LIMIT 1; #start 239 end 483 time 2017-07-19 01:02:49 DELETE FROM `mytest`.`users` WHERE `age`=32 AND `sex`='F' AND `id`=2 AND `name`='jak' LIMIT 1; #start 239 end 483 time 2017-07-19 01:02:49 DELETE FROM `mytest`.`users` WHERE `age`=45 AND `sex`='M' AND `id`=3 AND `name`='ses' LIMIT 1; #start 239 end 483 time 2017-07-19 01:02:49 DELETE FROM `mytest`.`users` WHERE `age`=35 AND `sex`='M' AND `id`=4 AND `name`='lisea' LIMIT 1; #start 239 end 483 time 2017-07-19 01:02:49
* binlog2sql通過flashback生成回滾SQL
[root@MySQL ~]# python binlog2sql/binlog2sql/binlog2sql.py -hlocalhost -P3306 -uroot -p'123' -dmytest -tusers --start-file='bin.000006' --start-position=239 --stop-position=483 -B > rollback.sql
* 導入回滾SQL [ 導入前檢查SQL語句是否正常 ]
[root@MySQL ~]# mysql -hlocalhost -uroot -p'123' < rollback.sql
* 查看
mysql> select * from mytest.users; +----+-------+-----+-----+ | id | name | sex | age | +----+-------+-----+-----+ | 1 | tom | M | 25 | | 2 | jak | F | 32 | | 3 | ses | M | 45 | | 4 | lisea | M | 35 | +----+-------+-----+-----+ 4 rows in set (0.00 sec)
4. 總結
以需求驅動技術,技術本身沒有優略之分,只有業務之分。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。