翼度科技»论坛 编程开发 mysql 查看内容

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

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
Github


MyFlash 限制
  1. 仅支持 <strong>5.6</strong> 与 <strong>5.7</strong>
  2. <strong>binlog</strong> 格式必须为 <strong>row</strong>,且 <strong>binlog_row_image=full</strong>
  3. 只能回滚DML(<strong>增、删、改</strong>)
复制代码
MySQL 准备
  1. <strong>注:</strong> 本章 <strong>MySQL</strong> 是采用 <strong>Docker</strong> 部署,容器内是 <strong>不带mysqlbinlog</strong> 的,可以从已安装 MySQL 的Linux服务器上拷贝 <strong>mysqlbinlog</strong> 文件。
复制代码
开启 binlog
  1. [mysqld]
  2. # binlog功能
  3. log_bin=/var/lib/mysql/mysql-bin
  4. # binlog 文件格式
  5. binlog_format=ROW
  6. binlog_row_image=FULL
  7. # binlog 文件保留时间7天(默认0天)
  8. expire_logs_days=7
  9. # binlog 单个文件的最大值大小(默认1G)
  10. max_binlog_size=512m
  11. # 开启 binlog 后需要创建 function 或 procedure 时要开启
  12. log_bin_trust_function_creators=1
  13. # 服务id,以区分主库和备库
  14. server-id=1
复制代码
  1. -- 显示是否开启 binlog
  2. show variables like 'log_bin%';
  3. -- 显示 binlog 文件格式
  4. show variables like 'binlog_format%';
  5. -- 显示 binlog 文件保留时间
  6. show variables like 'expire_logs_days%';
  7. -- 显示 binlog 单个文件的最大值大小
  8. show variables like 'max_binlog_size%';
复制代码
  1. -- 显示 binlog 事件
  2. show binlog events;
  3. -- 显示全部 binlog 文件列表
  4. show binary logs;
  5. show master logs;
  6. -- 显示最新 binlog 文件编号以及最后一个操作事件结束点(Position)值
  7. show master status;
  8. -- 结束当前 binlog 文件并生成新的 binlog 文件
  9. flush logs;
  10. -- 重置 binlog 文件(清空全部 biglog 文件)
  11. reset master;
复制代码
  1. <strong>注:</strong> <strong>binlog</strong> 默认存放在 <strong>/var/lib/mysql/</strong> 数据库文件目录。
复制代码
mysqlbinlog


  • 查看 Linux 环境下 mysqlbinlog 目录
  1. # /usr/bin/mysqlbinlog
  2. whereis mysqlbinlog
复制代码

  • 将 mysqlbinlog 文件复制到容器内的 /usr/bin 目录
  1. docker cp mysqlbinlog mysql:/usr/bin
  2. # 进入容器内
  3. docker exec -it mysql /bin/bash
  4. # 在容器内为 mysqlbinlog 添加权限
  5. chmod +x /usr/bin/mysqlbinlog
复制代码

  • 使用 mysqlbinlog 生成 sql 文件
  1. # 进入容器内执行
  2. mysqlbinlog --no-defaults --base64-output=DECODE-ROWS -v /var/lib/mysql/mysql-bin.000001 >/tmp/binlog-000001.sql
复制代码
  1. # 进入容器内执行
  2. mysqlbinlog --no-defaults \
  3. --database=db_name \
  4. --start-datetime='2024-06-20 00:00:00' \
  5. --stop-datetime='2024-06-20 17:00:00' \
  6. /var/lib/mysql/mysql-bin.000001 >/tmp/binlog-000001.sql
复制代码
安装 MyFlash


  • 安装依赖
  1. # 安装 gcc glib-2.0
  2. yum install -y gcc libgnomeui-devel
  3. # 验证是否安装成功
  4. pkg-config --modversion glib-2.0
复制代码

  • 安装MyFlash
  1. # 下载源码
  2. git clone https://github.com/Meituan-Dianping/MyFlash.git
  3. cd MyFlash
  4. # 动态编译链接安装
  5. gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback
复制代码

  • 配置环境变量
  1. vim /etc/profile
复制代码
  1. # 在文件末尾添加
  2. alias flashback=~/MyFlash/binary/flashback
复制代码
  1. source /etc/profile
复制代码

  • 验证是否安装成功
  1. cd ~/MyFlash/binary
  2. ./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, seperate 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(,)
生成回滚文件
  1. # 进入MyFlash的bin目录
  2. cd ~/MyFlash/binary
  3. # 生成回滚文件,执行后会在当前目录下生成 binlog_output_base.flashback 文件
  4. ./flashback --sqlTypes='INSERT' \
  5. --binlogFileNames=/var/lib/mysql/mysql-bin.000001 \
  6. --databaseNames=db_name \
  7. --tableNames=table_name \
  8. --start-datetime='2024-06-20 18:23:00'
复制代码
执行回滚操作
  1. # 在binlog_output_base.flashback所在目录下执行以下命令
  2. mysqlbinlog binlog_output_base.flashback | mysql -h 127.0.0.1 -uroot -p
  3. # 或
  4. mysqlbinlog binlog_output_base.flashback | mysql -uroot -p
  5. # 输入数据库密码
复制代码
到此这篇关于利用MyFlash实现MySQL数据闪回的操作指南的文章就介绍到这了,更多相关MyFlash MySQL数据闪回内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/database/323026qie.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具