您的位置:首页 > 科技 > 能源 > 利用 MyFlash 实现 MySQL 数据闪回

利用 MyFlash 实现 MySQL 数据闪回

2024/10/5 13:46:17 来源:https://blog.csdn.net/weixin_42607526/article/details/139843283  浏览:    关键词:利用 MyFlash 实现 MySQL 数据闪回

Github

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

MyFlash 限制

  1. 仅支持 5.65.7
  2. binlog 格式必须为 row,且 binlog_row_image=full
  3. 只能回滚DML(增、删、改

MySQL 准备

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

开启 binlog

[mysqld]
# binlog功能
log_bin=/var/lib/mysql/mysql-bin
# binlog 文件格式
binlog_format=ROW
# 表无论有没有主键约束或者唯一约束binlog都会记录所有前后镜像
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

注: 文章 MyFlash 是在 Linux 宿主机上安装。

  • 安装依赖
# 安装 gcc glib-2.0
yum install -y gcc glib2 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
# 输入数据库密码

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com