• 8.mysql闪回工具之myflash


    1.简介

      MyFlash是由美团点评公司技术工程部开发维护的一个回滚DML操作的工具。该工具通过解析v4版本的binlog,完成回滚操作。相对已有的回滚工具,其增加了更多的过滤选项,让回滚更加容易。 该工具已经在美团点评内部使用

    2.限制

      1.binlog格式必须为row,且binlog_row_image=full

          2.仅支持5.6与5.7

          3.只能回滚DML(增、删、改)

    3.原理

      https://mp.weixin.qq.com/s?__biz=MjM5NjQ5MTI5OA==&mid=2651747096&idx=1&sn=e561ce9254f69c3fcd04c73b44e56e69&chksm=bd12aa558a65234332298e1d611da408ec45afb2d08ecd22078a600379525e75b2fa13149fb0&mpshare=1&scene=23&srcid=1116o1gBWoNf47Rv1E1ngYir#rd

    4.安装

      4.1 安装依赖 

     yum -y  install gcc*  pkg-config glib2 libgnomeui-devel  glib2-devel 

      4.2 下载源码包

      这里到github上面下载,我这里用是下载到本地

      4.3 编译安装 

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

    5.使用方法

    [root@node01 binary]# ls
    flashback  mysqlbinlog20160408
    [root@node01 binary]# ./flashback  --help
    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(,)
      --tableNames-file           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. if multiple, seperate by comma(,)
      --include-gtids-file        gtids to process. if multiple, seperate by comma(,)
      --exclude-gtids             gtids to skip. if multiple, seperate by comma(,)
      --exclude-gtids-file        gtids to skip. if multiple, seperate by comma(,)

    6.参数解释

    下面的这些参数是可以任意组合的。

    • 1.databaseNames

      指定需要回滚的数据库名。多个数据库可以用“,”隔开。如果不指定该参数,相当于指定了所有数据库。

    • 2.tableNames

      指定需要回滚的表名。多个表可以用“,”隔开。如果不指定该参数,相当于指定了所有表。

    • 3.start-position

      指定回滚开始的位置。如不指定,从文件的开始处回滚。请指定正确的有效的位置,否则无法回滚

    • 4.stop-position

      指定回滚结束的位置。如不指定,回滚到文件结尾。请指定正确的有效的位置,否则无法回滚

    • 5.start-datetime

      指定回滚的开始时间。注意格式必须是 %Y-%m-%d %H:%M:%S。 如不指定,则不限定时间

    • 6.stop-datetime

      指定回滚的结束时间。注意格式必须是 %Y-%m-%d %H:%M:%S。 如不指定,则不限定时间

    • 7.sqlTypes

      指定需要回滚的sql类型。目前支持的过滤类型是INSERT, UPDATE ,DELETE。多个类型可以用“,”隔开。

    • 8.maxSplitSize

      一旦指定该参数,对文件进行固定尺寸的分割(单位为M),过滤条件有效,但不进行回滚操作。该参数主要用来将大的binlog文件切割,防止单次应用的binlog尺寸过大,对线上造成压力

    • 9.binlogFileNames

      指定需要回滚的binlog文件,目前只支持单个文件,后续会增加多个文件支持

    • 10.outBinlogFileNameBase

      指定输出的binlog文件前缀,如不指定,则默认为binlog_output_base.flashback

    • 11.logLevel

      仅供开发者使用,默认级别为error级别。在生产环境中不要修改这个级别,否则输出过多

    • 12.include-gtids

      指定需要回滚的gtid,支持gtid的单个和范围两种形式。

    • 13.exclude-gtids

      指定不需要回滚的gtid,用法同include-gtids

    7.测试用例

      1.基于单表(某一个事务号)进行数据误删除进行闪回

      开始前: 

    root@localhost 18:20:  [test]> select * from t;
    +----+------+
    | id | k    |
    +----+------+
    |  1 |    2 |
    |  2 |    2 |
    |  3 |    3 |
    +----+------+
    3 rows in set (0.00 sec)

      模拟执行操作后 

    root@localhost 18:21:  [test]> delete from t where id=3;
    Query OK, 1 row affected (0.01 sec)
    
    root@localhost 18:22:  [test]> commit;
    Query OK, 0 rows affected (0.00 sec)
    
    root@localhost 18:22:  [test]> select * from t;
    +----+------+
    | id | k    |
    +----+------+
    |  1 |    2 |
    |  2 |    2 |
    +----+------+

      用闪回工具进行闪回处理

      这里主要通过找位置点进行闪回(开始位置点和结束位置点或者通过事务号),这里是通过命令show binlog events in '二进制文件' 来进行查看  

    | bin.000026 | 1349 | Gtid           |     20107 |        1410 | SET @@SESSION.GTID_NEXT= '5d15c884-2750-11ec-92fd-000c29395ab1:169181' |
    | bin.000026 | 1410 | Query          |     20107 |        1478 | BEGIN                                                                  |
    | bin.000026 | 1478 | Table_map      |     20107 |        1519 | table_id: 110 (test.t)                                                 |
    | bin.000026 | 1519 | Delete_rows    |     20107 |        1559 | table_id: 110 flags: STMT_END_F                                        |
    | bin.000026 | 1559 | Xid            |     20107 |        1586 | COMMIT /* xid=95 */                                                    |
    +

      可以看到执行删除的这个事务号是:5d15c884-2750-11ec-92fd-000c29395ab1:169181 ,开始的位置是1349,结束的位置是1559.

      开始执行闪回操作: 

      1.生成回滚文件
        flashback  --binlogFileNames=/data/3307/binlog/bin.000026  --include-gtids='5d15c884-2750-11ec-92fd-000c29395ab1:169181'
      2.应用文件
        mysqlbinlog binlog_output_base.flashback   --skip-gtids |mysql -uroot -p123 -S /data/3307/mysqld_3307.sock

      结果

    root@localhost 18:50:  [test]> select * from t;
    +----+------+
    | id | k    |
    +----+------+
    |  1 |    2 |
    |  2 |    2 |
    |  3 |    3 |
    +----+------+

    。。。

      2.基于时间点进行闪回  

    flashback --databaseNames="d1" --tableNames="t1" --start-datetime="2020-11-29 17:22:30" --stop-datetime="2020-11-29 17:24:30" --sqlTypes="UPDATE,DELETE" --binlogFileNames=/opt/mysql/log/binlog/3308/mysql-bin.000001 --outBinlogFileNameBase=test.sql

    参考:

    https://github.com/Meituan-Dianping/MyFlash/blob/master/doc/how_to_use.md 

     

  • 相关阅读:
    洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers
    洛谷P2434 [SDOI2005]区间
    洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn
    洛谷 P2904 [USACO08MAR]跨河River Crossing
    Again Stone Game
    Crazy Calendar (阶梯博弈变形)
    (2017浙江省赛E)Seven Segment Display
    ZOJ 3705 Applications
    D
    The Suspects(并查集维护根节点信息)
  • 原文地址:https://www.cnblogs.com/zmc60/p/16349377.html
Copyright © 2020-2023  润新知