当前位置: 代码网 > it编程>数据库>Mysql > 利用MyFlash实现MySQL数据闪回的操作指南

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

2024年07月04日 Mysql 我要评论
githubhttps://github.com/meituan-dianping/myflashmyflash 限制仅支持5.6与5.7binlog格式必须为row,且binlog_row_imag

github

myflash 限制

仅支持 5.6 与 5.7

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

只能回滚dml(增、删、改

mysql 准备

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

开启 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 文件格式
show 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 install -y gcc libgnomeui-devel
# 验证是否安装成功
pkg-config --modversion glib-2.0
  • 安装myflash
# 下载源码
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
  • 配置环境变量
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, 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(,)

生成回滚文件

# 进入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数据闪回内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com