您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)MySQL中MyFlash如何安裝使用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
Myflash的安裝與使用
1、環(huán)境說明
1)centos 7.x
2)mysql5.7.21 限制binlog格式必須為row,且binlog_row_image=full
只支持DML的回滾(insert、delete、updte)
操作對象test01庫、users表
3)Myflash
2、安裝Myflash
1)安裝依賴包
yum install gcc* pkg-config glib2 libgnomeui-devel -y
2)安裝
git clone https://github.com/Meituan-Dianping/MyFlash.git
cd MyFlash
gcc -w pkg-config --cflags --libs glib-2.0 source/ binlogParseGlib.c -o binary/flashback
cd binary
./flashback --help
顯示幫助文檔即可說明安裝成功
3)設(shè)置環(huán)境變量
vim /etc/profile
最后一行添加
alias flashback=“/opt/MyFlash/binary/flashback"
source /etc/profile
3、使用:?下面的這些參數(shù)是可以任意組合的。
1.databaseNames 指定需要回滾的數(shù)據(jù)庫名。多個數(shù)據(jù)庫可以用“,”隔開。如果不指定該參數(shù),相當于指定了所有數(shù)據(jù)庫。 2.tableNames?指定需要回滾的表名。多個表可以用“,”隔開。如果不指定該參數(shù),相當于指定了所有表。
3.start-position?指定回滾開始的位置。如不指定,從文件的開始處回滾。請指定正確的有效的位置,否則無法回滾
4.stop-position?指定回滾結(jié)束的位置。如不指定,回滾到文件結(jié)尾。請指定正確的有效的位置,否則無法回滾
5.start-datetime?指定回滾的開始時間。注意格式必須是 %Y-%m-%d %H:%M:%S。 如不指定,則不限定時間
6.stop-datetime?指定回滾的結(jié)束時間。注意格式必須是 %Y-%m-%d %H:%M:%S。 如不指定,則不限定時間
7.sqlTypes?指定需要回滾的sql類型。目前支持的過濾類型是INSERT, UPDATE ,DELETE。多個類型可以用“,”隔開。
8.maxSplitSize?一旦指定該參數(shù),對文件進行固定尺寸的分割(單位為M),過濾條件有效,但不進行回滾操作。該參數(shù)主要用來將大的binlog文件切割,防止單次應(yīng)用的binlog尺寸過大,對線上造成壓力
9.binlogFileNames?指定需要回滾的binlog文件,目前只支持單個文件,后續(xù)會增加多個文件支持
10.outBinlogFileNameBase?指定輸出的binlog文件前綴,如不指定,則默認為binlog_output_base.flashback
11.logLevel?僅供開發(fā)者使用,默認級別為error級別。在生產(chǎn)環(huán)境中不要修改這個級別,否則輸出過多
12.include-gtids?指定需要回滾的gtid,支持gtid的單個和范圍兩種形式。
13.exclude-gtids?指定不需要回滾的gtid,用法同include-gtids
4、案例1
1)創(chuàng)建庫和表
use test01
Database changed
(root@localhost:mysql.sock) [test01]>create table users(
-> id bigint unsigned NOT NULL AUTO_INCREMENT,
-> realname varchar(20) not null comment '真實姓名',
-> age int not null default '20' comment '年齡',
-> primary key(id)
-> )engine=innodb default charset=utf8mb4 collate=utf8mb4_unicode_ci comment '測試用戶表';
Query OK, 0 rows affected (0.60 sec)
2)造數(shù)據(jù)
(root@localhost:mysql.sock) [test01]>insert into users (realname,age) values('kitten','25');
Query OK, 1 row affected (0.03 sec)
(root@localhost:mysql.sock) [test01]>insert into users (realname,age) select realname,age from user;
ERROR 1146 (42S02): Table 'test01.user' doesn't exist
(root@localhost:mysql.sock) [test01]>insert into users (realname,age) select realname,age from users;
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
(root@localhost:mysql.sock) [test01]>insert into users (realname,age) select realname,age from users;
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
(root@localhost:mysql.sock) [test01]>insert into users (realname,age) select realname,age from users;
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
(root@localhost:mysql.sock) [test01]>insert into users (realname,age) select realname,age from users;
Query OK, 8 rows affected (0.03 sec)
Records: 8 Duplicates: 0 Warnings: 0
(root@localhost:mysql.sock) [test01]>select count(*) from users;
+----------+
| count(*) |
+----------+
| 16 |
+----------+
1 row in set (0.00 sec)
(root@localhost:mysql.sock) [test01]>select * from users;
+----+----------+-----+
| id | realname | age |
+----+----------+-----+
| 1 | kitten | 25 |
| 2 | kitten | 25 |
| 3 | kitten | 25 |
| 4 | kitten | 25 |
| 6 | kitten | 25 |
| 7 | kitten | 25 |
| 8 | kitten | 25 |
| 9 | kitten | 25 |
| 13 | kitten | 25 |
| 14 | kitten | 25 |
| 15 | kitten | 25 |
| 16 | kitten | 25 |
| 17 | kitten | 25 |
| 18 | kitten | 25 |
| 19 | kitten | 25 |
| 20 | kitten | 25 |
+----+----------+-----+
16 rows in set (0.00 sec)
3)刪數(shù)據(jù)
delete from user where id<10;
(root@localhost:mysql.sock) [test01]>select
-> * from users;
+----+----------+-----+
| id | realname | age |
+----+----------+-----+
| 13 | kitten | 25 |
| 14 | kitten | 25 |
| 15 | kitten | 25 |
| 16 | kitten | 25 |
| 17 | kitten | 25 |
| 18 | kitten | 25 |
| 19 | kitten | 25 |
| 20 | kitten | 25 |
+----+----------+-----+
8 rows in set (0.00 sec)
5)查看binlog 確認start position \stop position
#mysqlbinlog /data/mysqldata/mysql-bin.000005 –base64-output=decode-rows -v
#180308 13:35:46 server id 223 end_log_pos 29355 CRC32 0x1b4db5f5 Query thread_id=692 exec_time=0 error_code=0
SET TIMESTAMP=1520487346/*!*/;
BEGIN
/*!*/;
# at 29355
#180308 13:35:46 server id 223 end_log_pos 29409 CRC32 0x662b1568 Table_map: `test01`.`users` mapped to number 117
# at 29409
#180308 13:35:46 server id 223 end_log_pos 29604 CRC32 0x4e984497 Delete_rows: table id 117 flags: STMT_END_F
BINLOG '
ssugWhPfAAAANgAAAOFyAAAAAHUAAAAAAAEABnRlc3QwMQAFdXNlcnMAAwgPAwJQAABoFStm
ssugWiDfAAAAwwAAAKRzAAAAAHUAAAAAAAEAAgAD//gBAAAAAAAAAAZraXR0ZW4ZAAAA+AIAAAAA
AAAABmtpdHRlbhkAAAD4AwAAAAAAAAAGa2l0dGVuGQAAAPgEAAAAAAAAAAZraXR0ZW4ZAAAA+AYA
AAAAAAAABmtpdHRlbhkAAAD4BwAAAAAAAAAGa2l0dGVuGQAAAPgIAAAAAAAAAAZraXR0ZW4ZAAAA
+AkAAAAAAAAABmtpdHRlbhkAAACXRJhO
'/*!*/;
### DELETE FROM `test01`.`users`
### WHERE
### @1=1
### @2='kitten'
### @3=25
### DELETE FROM `test01`.`users`
### WHERE
### @1=2
### @2='kitten'
### @3=25
### DELETE FROM `test01`.`users`
### WHERE
### @1=3
### @2='kitten'
### @3=25
### DELETE FROM `test01`.`users`
### WHERE
### @1=4
### @2='kitten'
### @3=25
### DELETE FROM `test01`.`users`
### WHERE
### @1=6
### @2='kitten'
### @3=25
### DELETE FROM `test01`.`users`
### WHERE
### @1=7
### @2='kitten'
### @3=25
### DELETE FROM `test01`.`users`
### WHERE
### @1=8
### @2='kitten'
### @3=25
### DELETE FROM `test01`.`users`
### WHERE
### @1=9
### @2='kitten'
### @3=25
# at 29604
#180308 13:35:46 server id 223 end_log_pos 29635 CRC32 0x443a1025 Xid = 4813
COMMIT/*!*/;
mysqlbinlog: File '–base64-output=decode-rows' not found (Errcode: 2 - No such file or directory)
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
Start-positon=29216
Stop-position=29635
6)生成binlog日志
flashback —binlogFileNames=/data/mysqldata/mysql-bin.00005 —start-position= 29216—stop-position=29635
默認生成一個binlog_output_base.flashback文件
7)恢復(fù)
mysqlbinlog —skip-gtids /opt/MyFlash/binary/binlog_output_base.flashbak|mysql -uroot -p
8)查看數(shù)據(jù)
(root@localhost:mysql.sock) [test01]>select * from users;
+----+----------+-----+
| id | realname | age |
+----+----------+-----+
| 1 | kitten | 25 |
| 2 | kitten | 25 |
| 3 | kitten | 25 |
| 4 | kitten | 25 |
| 6 | kitten | 25 |
| 7 | kitten | 25 |
| 8 | kitten | 25 |
| 9 | kitten | 25 |
| 13 | kitten | 25 |
| 14 | kitten | 25 |
| 15 | kitten | 25 |
| 16 | kitten | 25 |
| 17 | kitten | 25 |
| 18 | kitten | 25 |
| 19 | kitten | 25 |
| 20 | kitten | 25 |
+——+----------+-----+
確認已經(jīng)刪除的數(shù)據(jù)已經(jīng)恢復(fù)
5、案例2 —測試updte、delete恢復(fù)
1)修改數(shù)據(jù)
(root@localhost:mysql.sock) [test01]>update users set realname='panxiao',age=28 where id in (1,2,3);
Query OK, 3 rows affected (0.03 sec)
Rows matched: 3 Changed: 3 Warnings: 0
(root@localhost:mysql.sock) [test01]>delete from users where id =8
2)查看當前的數(shù)據(jù)
(root@localhost:mysql.sock) [test01]>select * from users;
+----+----------+-----+
| id | realname | age |
+----+----------+-----+
| 1 | panxiao | 28 |
| 2 | panxiao | 28 |
| 3 | panxiao | 28 |
| 4 | kitten | 25 |
| 6 | kitten | 25 |
| 7 | kitten | 25 |
| 9 | kitten | 25 |
| 13 | kitten | 25 |
| 14 | kitten | 25 |
| 15 | kitten | 25 |
| 16 | kitten | 25 |
| 17 | kitten | 25 |
| 18 | kitten | 25 |
| 19 | kitten | 25 |
| 20 | kitten | 25 |
+——+----------+-----+
3?3)生成恢復(fù)sql
flashback --binlogFileNames=/data/mysqldata/mysql-bin.000005 --start-datetime="2018-03-08 14:13:00" --stop-datetime="2018-03-08 14:23:00" --databaseNames=test01 --tableNames=users --sqlTypes='UPDATE','DELETE' —outBinlogFileNameBase=test01_users
4)查看生成的binlog
#mysqlbinlog --no-defaults --base64-output=decode-row -vv test01_users.flashback
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLE TION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180306 20:14:47 server id 223 end_log_pos 123 CRC32 0xb78347d8 Start: binlog v 4, server v 5.7.21-log created 180306 20:14:47 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
# at 123
#180308 14:19:18 server id 223 end_log_pos 177 CRC32 0x5fd8b365 Table_map: `test01`.`users` mapped to number 117
# at 177
#180308 14:19:18 server id 223 end_log_pos 232 CRC32 0x608a735a Write_rows: table id 117 flags: STMT_END_F
### INSERT INTO `test01`.`users`
### SET
### @1=8 /* LONGINT meta=0 nullable=0 is_null=0 */
### @2='kitten' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
### @3=25 /* INT meta=0 nullable=0 is_null=0 */
# at 232
#180308 14:17:56 server id 223 end_log_pos 286 CRC32 0xf48cb3b5 Table_map: `test01`.`users` mapped to number 117
# at 286
#180308 14:17:56 server id 223 end_log_pos 445 CRC32 0xcd45ef63 Update_rows: table id 117 flags: STMT_END_F
### UPDATE `test01`.`users`
### WHERE
### @1=1 /* LONGINT meta=0 nullable=0 is_null=0 */
### @2='panxiao' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
### @3=28 /* INT meta=0 nullable=0 is_null=0 */
### SET
### @1=1 /* LONGINT meta=0 nullable=0 is_null=0 */
### @2='kitten' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
### @3=25 /* INT meta=0 nullable=0 is_null=0 */
### UPDATE `test01`.`users`
### WHERE
### @1=2 /* LONGINT meta=0 nullable=0 is_null=0 */
### @2='panxiao' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
### @3=28 /* INT meta=0 nullable=0 is_null=0 */
### SET
### @1=2 /* LONGINT meta=0 nullable=0 is_null=0 */
### @2='kitten' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
### @3=25 /* INT meta=0 nullable=0 is_null=0 */
### UPDATE `test01`.`users`
### WHERE
### @1=3 /* LONGINT meta=0 nullable=0 is_null=0 */
### @2='panxiao' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
### @3=28 /* INT meta=0 nullable=0 is_null=0 */
### SET
### @1=3 /* LONGINT meta=0 nullable=0 is_null=0 */
### @2='kitten' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
### @3=25 /* INT meta=0 nullable=0 is_null=0 */
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
4)恢復(fù)sql
mysqlbinlog --no-defaults test01_users.flashback |mysql -uroot -p
5)查看數(shù)據(jù) —已經(jīng)恢復(fù)
(root@localhost:mysql.sock) [test01]>select * from users;
+----+----------+-----+
| id | realname | age |
+----+----------+-----+
| 1 | kitten | 25 |
| 2 | kitten | 25 |
| 3 | kitten | 25 |
| 4 | kitten | 25 |
| 6 | kitten | 25 |
| 7 | kitten | 25 |
| 8 | kitten | 25 |
| 9 | kitten | 25 |
| 13 | kitten | 25 |
| 14 | kitten | 25 |
| 15 | kitten | 25 |
| 16 | kitten | 25 |
| 17 | kitten | 25 |
| 18 | kitten | 25 |
| 19 | kitten | 25 |
| 20 | kitten | 25 |
感謝各位的閱讀!關(guān)于“MySQL中MyFlash如何安裝使用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。