安装binlog2sql

binlog2sql支持python2和python3,centos7自带的python就能正常运行,只是需要安装一些依赖。

1
2
3
4
5
6
yum install git pip
#pip安装不上可以改成python-pip,首先需要yum install epel-release
yum install epel-release
yum install python-pip

git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sql

根据官方教程,这里使用pip安装相关依赖,但是经本人踩坑,这里的依赖版本有问题,会导致生成的sql字段错位,需要调整版本mysql-replication>0.19,这是我用的requirements.txt版本,实测恢复mysql8数据没什么问题

1
2
3
PyMySQL==0.9.3
wheel==0.29.0
mysql-replication==0.21

修改完requirements.txt文件后,安装依赖

1
pip install -r requirements.txt 

mysql配置

这是官方要求的mysql配置,但是我发现我的mysql配置跟要求有一定出入,也能用。。。

1
2
3
4
5
6
[mysqld]
server_id = 1
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 1G
binlog_format = row
binlog_row_image = full

这是我的mysql配置文件,跟官方的有一定出入,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

#[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock

#log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid

[mysqld]
#skip-grant-tables
log-bin=mysql-bin
server-id=13

default-authentication-plugin = mysql_native_password
pid-file = /data/var/run/mysqld/mysqld.pid
socket = /data/var/run/mysqld/mysqld.sock
datadir = /data/var/lib/mysql
secure-file-priv = NULL
symbolic-links = 0
max_connections = 2000
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
# 表名不区分大小写(启动前配置)
lower_case_table_names = 1
# #设置日志时区和系统一致
log_timestamps = SYSTEM
skip-name-resolve
innodb_buffer_pool_size = 16G
#slave-skip-errors=all
[client]
default-character-set = utf8mb4
socket = /data/var/run/mysqld/mysqld.sock

连接数据的库的账号要求最少以下权限,不过一般都直接root连了

1
2
3
4
select, super/replication client, replication slave

建议授权
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO

使用

1
2
3
# 要先进入有binlog2sql.py的目录
cd binlog2sql
python binlog2sql.py --flashback -h10.0.10.79 -P3306 -uroot -p'123123' -d testdata -t testtable --start-file='mysql-bin.000110'

默认会将内容都直接输出,有时候操作比较多看起来就很累,可以先输出到文件,可以直接执行这个sql,也可以先看看对不对,万一又有字段错位之类坑爹问题。

1
2
python binlog2sql.py --flashback -h10.0.10.79 -P3306 -uroot -p'123123' -d testdata -t testtable --start-file='mysql-bin.000110' >> flashback.sql

更多使用案例,参考官方文档了
binlog2sql/mysql-flashback-priciple-and-practice.md at master · danfengcao/binlog2sql (github.com)

选项

mysql连接配置

-h host; -P port; -u user; -p password

解析模式

–stop-never 持续解析binlog。可选。默认False,同步至执行命令时最新的binlog位置。

-K, –no-primary-key 对INSERT语句去除主键。可选。默认False

-B, –flashback 生成回滚SQL,可解析大文件,不受内存限制。可选。默认False。与stop-never或no-primary-key不能同时添加。

–back-interval -B模式下,每打印一千行回滚SQL,加一句SLEEP多少秒,如不想加SLEEP,请设为0。可选。默认1.0。

解析范围控制

–start-file 起始解析文件,只需文件名,无需全路径 。必须。

–start-position/–start-pos 起始解析位置。可选。默认为start-file的起始位置。

–stop-file/–end-file 终止解析文件。可选。默认为start-file同一个文件。若解析模式为stop-never,此选项失效。

–stop-position/–end-pos 终止解析位置。可选。默认为stop-file的最末位置;若解析模式为stop-never,此选项失效。

–start-datetime 起始解析时间,格式’%Y-%m-%d %H:%M:%S’。可选。默认不过滤。

–stop-datetime 终止解析时间,格式’%Y-%m-%d %H:%M:%S’。可选。默认不过滤。

对象过滤

-d, –databases 只解析目标db的sql,多个库用空格隔开,如-d db1 db2。可选。默认为空。

-t, –tables 只解析目标table的sql,多张表用空格隔开,如-t tbl1 tbl2。可选。默认为空。

–only-dml 只解析dml,忽略ddl。可选。默认False。

–sql-type 只解析指定类型,支持INSERT, UPDATE, DELETE。多个类型用空格隔开,如–sql-type INSERT DELETE。可选。默认为增删改都解析。用了此参数但没填任何类型,则三者都不解析。

参考
danfengcao/binlog2sql: Parse MySQL binlog to SQL you want (github.com)