Mysql
主页 > 数据库 > Mysql >

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

2024-06-21 | 佚名 | 点击:

Github

MyFlash 限制

仅支持 5.6 与 5.7

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

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

MySQL 准备

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

开启 binlog

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[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

1

2

3

4

5

6

7

8

-- 显示是否开启 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%';

1

2

3

4

5

6

7

8

9

10

11

-- 显示 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

1

2

# /usr/bin/mysqlbinlog

whereis mysqlbinlog

1

2

3

4

5

docker cp mysqlbinlog mysql:/usr/bin

# 进入容器内

docker exec -it mysql /bin/bash

# 在容器内为 mysqlbinlog 添加权限

chmod +x /usr/bin/mysqlbinlog

1

2

# 进入容器内执行

mysqlbinlog --no-defaults --base64-output=DECODE-ROWS -v /var/lib/mysql/mysql-bin.000001 >/tmp/binlog-000001.sql

1

2

3

4

5

6

# 进入容器内执行

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

1

2

3

4

# 安装 gcc glib-2.0

yum install -y gcc libgnomeui-devel

# 验证是否安装成功

pkg-config --modversion glib-2.0

1

2

3

4

5

# 下载源码

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

1

vim /etc/profile

1

2

# 在文件末尾添加

alias flashback=~/MyFlash/binary/flashback

1

source /etc/profile

1

2

cd ~/MyFlash/binary

./flashback --help

flashback 选项

选项 说明
–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(,)

生成回滚文件

1

2

3

4

5

6

7

8

# 进入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'

执行回滚操作

1

2

3

4

5

# 在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

# 输入数据库密码

原文链接:
相关文章
最新更新