1.简介

        binlog2sql是大众点评开源的一款用于解析binlog的工具,在测试环境试用了下,还不错。

        关于该工具的使用方法可参考github操作文档:https://github.com/danfengcao/binlog2sql

        binlog2sql的功能:

                1. 提取SQL

                2. 生成回滚SQL

        binlog2sql的应用:

                数据快速回滚(闪回)

                主从切换后数据不一致的修复

                从binlog生成标准SQL,带来的衍生功能

2安装

        该工具的使用依赖以下三个包:

                PyMySQL==0.7.8

                wheel==0.24.0

                mysql-replication==0.9

        其中,每个包又会依赖其它包,所以安装这些包是一个比较麻烦的事情。

2.1 外网环境安装

        可直接通过pip install安装,它会自动下载并安装依赖包的。

        2.1.1 安装binlog2sql前先安装git和pip:

                yum -y install epel-release

                yum -y install git  python-pip

        2.1.2 安装binlog2sql:

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

                pip install -r requirements.txt

2.2 内网环境安装

        可手动安装这些包,目前,这些包已下载打包,并上传到百度云盘中,大家可自行下载。

        链接:https://pan.baidu.com/s/1lC3tEB27JSWAL3-ukr19oA 
        提取码:ange

        安装步骤:

                # tar xvf binlog2sql.tar.gz

                # cd binlog2sql/binlog2sql_dependencies/

                # tar xvf setuptools-0.6c11.tar.gz

                # cd setuptools-0.6c11

                # python setup.py install

                # cd ..

                # tar xvf pip-9.0.1.tar.gz

                # cd pip-9.0.1

                # python setup.py install

                # cd ..

                # pip install *.whl mysql-replication-0.9.tar.gz

                至此,所有依赖包安装完毕。

3 使用

        3.1使用该工具的前提

                3.1. binlog_format为ROW,且binlog_row_image为full或noblog,默认为full。

                3.2. 必须开启MySQL Server,理由有如下两点:

                   1> 它是基于BINLOG_DUMP协议来获取binlog内容

                   2> 需要读取server端information_schema.COLUMNS表,获取表结构的元信息,拼接成可视化的sql语句

                3.3 该工具所需权限如下:

                        GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO

                        因为是伪装成slave来获取主的二进制事件,故无需对binlog有可读权限。

        3.2 提取SQL示例

                python binlog2sql.py -h数据库IP -P3306 -u数据库账号 -p数据库密码 -d数据库 -t表 --start-file='binlog文件'

        3.3 生成回滚SQL示例

                python binlog2sql.py --flashback -h数据库IP -P3306 -u数据库账号 -p数据库密码 -d数据库 -t表 --start-file='binlog文件'

        3.4 参数

            

python binlog2sql.py --help
                usage: binlog2sql.py [-h HOST] [-u USER] [-p PASSWORD] [-P PORT]
                      [--start-file STARTFILE] [--start-position STARTPOS]
                      [--stop-file ENDFILE] [--stop-position ENDPOS]
                      [--start-datetime STARTTIME] [--stop-datetime STOPTIME]
                      [--stop-never] [--help] [-d [DATABASES [DATABASES ...]]]
                      [-t [TABLES [TABLES ...]]] [-K] [-B]                Parse MySQL binlog to SQL you want

        optional arguments:

--stop-never

Wait for more data from the server. default: stop replicate at the last binlog when you start binlog2sql

##持续同步binlog。可选。不加则同步至执行命令时最新的binlog位置

--help

help information

-K, --no-primary-key

Generate insert sql without primary key if exists

##对INSERT语句去除主键。可选。

-B, --flashback

Flashback data to start_postition of start_file

##生成回滚语句,可解析大文件,不受内存限制,每打印一千行加一句SLEEP SELECT(1)。可选。与stop-never或no-primary-key不能同时添加。

        connect setting:

-h HOST, --host HOST

Host the MySQL database server located

-u USER, --user USER

MySQL Username to log in as

-p PASSWORD, --password PASSWORD

MySQL Password to use

-P PORT, --port PORT

MySQL port to use

        range filter:

--start-file STARTFILE

Start binlog file to be parsed

##起始解析文件。必须。

--start-position STARTPOS, --start-pos STARTPOS

 ##start-file的起始解析位置。可选。默认为start-file的起始位置。

Start position of the --start-file

--stop-file ENDFILE, --end-file ENDFILE

##末尾解析文件。可选。默认为start-file同一个文件。若解析模式为stop-never,此选项失效。

Stop binlog file to be parsed. default: '--start-file'

--stop-position ENDPOS, --end-pos ENDPOS

##stop-file的末尾解析位置。可选。默认为stop-file的最末位置;若解析模式为stop-never,此选项失效。

Stop position of --stop-file. default: latest position of '--stop-file'

--start-datetime STARTTIME

Start reading the binlog at first event having a datetime equal or posterior to the argument; the argument must be a date and time in the local time zone, in any format accepted by the MySQL server for DATETIME and TIMESTAMP types, for example: 2004-12-25 11:25:56 (you should probably use quotes for your shell to set it properly)

##从哪个时间点的binlog开始解析,格式必须为datetime,如'2016-11-11 11:11:11'。可选。默认不过滤。

--stop-datetime STOPTIME

Stop reading the binlog at first event having a datetime equal or posterior to the argument; the argument must be a date and time in the local time zone, in any format accepted by the MySQL server for DATETIME and TIMESTAMP types, for example: 2004-12-25 11:25:56 (you should probably use quotes for your shell to set it properly).

##到哪个时间点的binlog停止解析,格式必须为datetime,如'2016-11-11 11:11:11'。可选。默认不过滤。

        schema filter:

-d [DATABASES [DATABASES ...]], --databases [DATABASES [DATABASES ...]]

dbs you want to process

##只输出目标db的sql。可选。默认为空。

-t [TABLES [TABLES ...]], --tables [TABLES [TABLES ...]]

tables you want to process

##只输出目标tables的sql。可选。默认为空。