开发者

利用MyFlash实现MySQL数据闪回的操作指南

目录
  • github
  • MyFlash 限制
  • mysql 准备
    • 开启 binlog
    • mysqlbinlog
  • 安装 MyFlash
    • flashback 选项
      • 生成回滚文件
        • 执行回滚操作

          Github

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

          MyFlash 限制

          仅支持 5.6 与 5.7

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

          只能回滚DML(增、删、改

          MySQL 准备

          注: 本章 MySQL 是采用 docker 部署,容器内是 不带mysqlbinlog 的,可以从已安装 MySQL 的linux服务器上拷贝 mysqlbinlog&androidnbsp;文件。

          开启 binlog

          [mysqld]
          # binlog功能
          log_bin=/var/lib/mysql/mysql-bin
          # binlog 文件格式
          binlog_format=ROW
          binlog_row_image=FULL
          # binlog 文件保留时间7天(默认0天)
          expire_logs_days=7
          # binlog 单个文件的最大值大小(默认1G)
          max_binlog_size=512m
          # 开启 binlog 后需要创建 function 或 procedure 时要开启
          log_bin_trust_function_creators=1
          # 服务id,以区分主库和备库
          server-id=1
          
          -- 显示是否开启 binlog
          show variables like 'log_bin%';
          -- 显示 binlog 文件格式
          sh编程客栈ow variables like 'binlog_format%';
          -- 显示 binlog 文件保留时间
          show variables like 'expire_logs_days%';
          -- 显示 binlog 单个文件的最大值大小
          show variables like 'max_binlog_size%';
          
          -- 显示 binlog 事件
          show binlog events;
          -- 显示全部 binlog 文件列表
          show binary logs;
          show master logs;
          -- 显示最新 binlog 文件编号以及最后一个操作事件结束点(Position)值
          show master status;
          -- 结束当前 binlog 文件并生成新的 binlog 文件
          flush logs;
          -- 重置 binlog 文件(清空全部 biglog 文件)
          reset master;
          

          注: binlog 默认存放在 /var/lib/mysql/ 数据库文件目录。

          mysqlbinlog

          • 查看 Linux 环境下 mysqlbinlog 目录
          # /usr/bin/mysqlbinlog
          whereis mysqlbinlog
          
          • 将 mysqlbinlog 文件复制到容器内的 /usr/bin 目录
          docker cp mysqlbinlog mysql:/usr/bin
          # 进入容器内
          docker exec -it mysql /bin/bash
          # 在容器内为 mysqlbinlog 添加权限
          chmod +x /usr/bin/mysqlbinlog
          
          • 使用 mysqlbinlog 生成 sql 文件
          # 进入容器内执行
          mysqlbinlog --no-defaults --base64-output=DECODE-ROWS -v /var/lib/mysql/mysql-bin.000001 >/tmp/binlog-000001.sql
          
          # 进入容器内执行
          mysqlbinlog --no-defaults \
          --database=db_name \
          --start-datetime='2024-06-20 00:00:00' \
          --stop-datetime='2024-06-20 17:00:00' \
          /var/lib/mysql/mysql-bin.000001 >/tmp/binlog-000001.sql
          

          安装 MyFlash

          • 安装依赖
          # 安装 gcc glib-2.0
          yum javascriptinstall -y gcc libgnomeui-devel
          # 验证是否安装成功
          pkg-config --modversion glib-2.0
          
          • 安装MyFlash
          # 下载源码
          git clone htt编程客栈ps://github.com/Meituan-Dianping/MyFlash.git
          cd MyFlash
          # 动态编译链接安装
          gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback
          
          • 配置环境变量
          vim /etc/profile
          
          # 在文件末尾添加
          alias flashback=~/MyFlash/binary/flashback
          
          source /etc/profile
          
          • 验证是否安装成功
          cd ~/MyFlash/binary
          ./flashback --help
          

          flashback 选项

          选项说明
          –databaseNamesdatabaseName to apply. if multiple, seperate by comma(,)
          –tableNamestableName to apply. if multiple, seperate by comma(,)
          –tableNames-filetableName to apply. if multiple, seperate by comma(,)
          –start-positionstart position
          –stop-positionstop position
          –start-datetimestart time (format %Y-%m-%d %H:%M:%S)
          –stop-datetimestop time (format %Y-%m-%d %H:%M:%S)
          –sqlTypessql type to filter . support INSERT, UPDATE ,DELETE. if multiple, seperate by comma(,)
          –maxSplitSizemax file size after split, the uint is M
          –binlogFileNamesbinlog files to process. if multiple, seperate by comma(,)
          –outBinlogFileNameBaseoutput binlog file name base
          –logLevellog level, available option is debug,warning,error
          –include-gtidsgtids to process. if multiple, sewww.devze.comperate by comma(,)
          –include-gtids-filegtids to process. if multiple, seperate by comma(,)
          –exclude-gtidsgtids to skip. if multiple, seperate by comma(,)
          –exclude-gtids-filegtids to skip. if multiple, seperate by comma(,)

          生成回滚文件

          # 进入MyFlash的bin目录
          cd ~/MyFlash/binary
          # 生成回滚文件,执行后会在当前目录下生成 binlog_output_base.flashback 文件
          ./flashback --sqlTypes='INSERT' \
          --binlogFileNames=/var/lib/mysql/mysql-bin.000001 \
          --databaseNames=db_name \
          --tableNames=table_name \
          --start-datetime='2024-06-20 18:23:00'
          

          执行回滚操作

          # 在binlog_output_base.flashback所在目录下执行以下命令
          mysqlbinlog binlog_output_base.flashback | mysql -h 127.0.0.1 -uroot -p
          # 或
          mysqlbinlog binlog_output_base.flashback | mysql -uroot -p
          # 输入数据库密码
          

          到此这篇关于利用MyFlash实现MySQL数据闪回的操作指南的文章就介绍到这了,更多相关MyFlash MySQL数据闪回内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

          0

          上一篇:

          下一篇:

          精彩评论

          暂无评论...
          验证码 换一张
          取 消

          最新数据库

          数据库排行榜