[美團] Myflash 的安裝使用


[美團] Myflash 的安裝使用

GitHub:

https://github.com/Meituan-Dianping/MyFlash

Myflash 相對於binlog2sql 和 mysqlbinlog 來說恢復速度非常快。

實現原理可以參考:http://url.cn/5yVTfLY

該方式不像binlog2sql 一樣轉換binlog為易讀的sql 語句,而是直接截取復制並修改二進制 binlog 文件實現SQL的反轉,然后使用mysqlbinlog 命令讀取新生成的二進制binlog閃回文件,將閃回操作導入數據庫實現數據的恢復。

使用過程中需要特別注意的是binlog 文件的位置一定不能出錯,注意相關參數的使用。

限制

  1. binlog格式必須為row,且binlog_row_image=full
  2. 僅支持5.6與5.7
  3. 只能回滾DML(增、刪、改)

安裝

yum install -y gcc glib2 glib2-devel

unzip MyFlash-master.zip

cd MyFlash-master

gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback

cd binary

[root@mysql1 binary]# ./flashback -h
Usage:
  flashback [OPTION?]

Help Options:
  -h, --help                  Show help options

Application Options:
  --databaseNames             databaseName to apply. if multiple, seperate by comma(,)
  --tableNames                tableName to apply. if multiple, seperate by comma(,)
  --start-position            start position
  --stop-position             stop position
  --start-datetime            start time (format %Y-%m-%d %H:%M:%S)
  --stop-datetime             stop time (format %Y-%m-%d %H:%M:%S)
  --sqlTypes                  sql type to filter . support INSERT, UPDATE ,DELETE. if multiple, seperate by comma(,)
  --maxSplitSize              max file size after split, the uint is M
  --binlogFileNames           binlog files to process. if multiple, seperate by comma(,)  
  --outBinlogFileNameBase     output binlog file name base
  --logLevel                  log level, available option is debug,warning,error
  --include-gtids             gtids to process
  --exclude-gtids             gtids to skip

測試案例

1. 建庫建表

-- 建庫
create database cym; use cym;
-- 建表
CREATE TABLE `t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB charset=utf8mb4;
-- 插入數據
flush logs;
insert into t1 values (1,'a'),(2,'b');
flush logs;
-- 獲取Binlog位置
show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000022 |       504 |
| mysql-bin.000023 |       194 |
+------------------+-----------+
select @@log_bin_basename;
+-----------------------------+
| @@log_bin_basename          |
+-----------------------------+
| /mysqldata/binlog/mysql-bin |
+-----------------------------+

2. 測試閃回insert

# 生成閃回binlog文件
rm -rf binlog_output_base.flashback
./binary/flashback --binlogFileNames=/mysqldata/binlog/mysql-bin.000022

# 執行閃回
mysqlbinlog --skip-gtids  binlog_output_base.flashback | mysql -uroot -proot cym

# 檢查驗證
mysql -uroot -proot cym
select * from t1;
Empty set (0.00 sec)

3. 測試閃回delete

# 生成閃回binlog文件
rm -rf binlog_output_base.flashback
./binary/flashback --binlogFileNames=/mysqldata/binlog/mysql-bin.000023 --sqlTypes=DELETE

# 執行閃回
mysqlbinlog --skip-gtids  binlog_output_base.flashback | mysql -uroot -proot cym

# 檢查驗證
mysql -uroot -proot cym -e 'select * from t1;'
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
+----+------+

4. 測試閃回update

-- 更新數據
mysql -uroot -proot cym
update t1 set name='c' where id=2;
select * from t1;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | c    |
+----+------+
2 rows in set (0.00 sec)
# 生成閃回binlog文件
rm -rf binlog_output_base.flashback
./binary/flashback --binlogFileNames=/mysqldata/binlog/mysql-bin.000023 --sqlTypes=update

# 執行閃回
mysqlbinlog --skip-gtids  binlog_output_base.flashback | mysql -uroot -proot cym

# 檢查驗證
mysql -uroot -proot cym -e 'select * from t1;'
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
+----+------+

5. 其他參數的測試使用

5.1 數據准備

flush logs;
create database cym1;use cym1;
create table t2 like cym.t1;
insert into t2 values(3,'c'),(4,'d'),(5,'e');

delete from t2 where id=4;				-- 需閃回的操作
insert into t2 values(6,'g');
insert into cym.t1 values(10,'test');
update t2 set name='f' where id=3;		-- 需閃回的操作

delete from t2 where id=5;
select * from cym.t1;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
| 10 | test |
+----+------+

select * from t2;
+----+------+
| id | name |
+----+------+
|  3 | f    |
|  6 | g    |
+----+------+

5.2 測試目標

  1. 恢復id=4的數據,
  2. 將id=3 恢復到修改前
  3. 不閃回其他操作,如對id=6 的insert和對cym.t1 表的變更。

目標:

select * from cym.t1;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
| 10 | test |
+----+------+

select * from t2;
+----+------+
| id | name |
+----+------+
|  3 | c    |
|  4 | d    |
|  6 | g    |
+----+------+

5.3 確認要恢復事務的GTID

show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000027 |     2084 |              |                  | fa9a20b5-831c-11ea-b919-080027a0316a:1-93 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

[root@mysql1 MyFlash-master]# mysqlbinlog -v /mysqldata/binlog/mysql-bin.000027 | egrep -i 'GTID_NEXT|UPDATE `cym1`.`t2`|DELETE FROM `cym1`.`t2`'
SET @@SESSION.GTID_NEXT= 'fa9a20b5-831c-11ea-b919-080027a0316a:86'/*!*/;
SET @@SESSION.GTID_NEXT= 'fa9a20b5-831c-11ea-b919-080027a0316a:87'/*!*/;
SET @@SESSION.GTID_NEXT= 'fa9a20b5-831c-11ea-b919-080027a0316a:88'/*!*/;
SET @@SESSION.GTID_NEXT= 'fa9a20b5-831c-11ea-b919-080027a0316a:89'/*!*/;
### DELETE FROM `cym1`.`t2`
SET @@SESSION.GTID_NEXT= 'fa9a20b5-831c-11ea-b919-080027a0316a:90'/*!*/;
SET @@SESSION.GTID_NEXT= 'fa9a20b5-831c-11ea-b919-080027a0316a:91'/*!*/;
SET @@SESSION.GTID_NEXT= 'fa9a20b5-831c-11ea-b919-080027a0316a:92'/*!*/;
### UPDATE `cym1`.`t2`
SET @@SESSION.GTID_NEXT= 'fa9a20b5-831c-11ea-b919-080027a0316a:93'/*!*/;
### DELETE FROM `cym1`.`t2`
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;

要恢復的GTID 為: fa9a20b5-831c-11ea-b919-080027a0316a:89-92 只恢復其中對t2 表的delete和update 操作

5.4 生成閃回binlog文件

rm -rf binlog_output_base.flashback
./binary/flashback --binlogFileNames=/mysqldata/binlog/mysql-bin.000027 --databaseNames=cym1 --tableNames=t2 --sqlTypes=delete,update --include-gtids='fa9a20b5-831c-11ea-b919-080027a0316a:89-92'

5.5 恢復並驗證

-- 恢復
mysqlbinlog --skip-gtids  binlog_output_base.flashback >flash.sql
mysql -uroot -proot
set sql_log_bin=0;
source flash.sql;
set sql_log_bin=1;

-- 驗證
select * from cym.t1;select * from cym1.t2;'
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
| 10 | test |
+----+------+
+----+------+
| id | name |
+----+------+
|  3 | c    |
|  4 | d    |
|  6 | g    |
+----+------+

符合預期,恢復結束。

通過以上的驗證,該恢復方式部署簡單,效率高,且恢復可靠。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM