Klustron(原KunlunBase) 的 Binlog2sync 工具 introduce
Klustron(原KunlunBase) 的 Binlog2sync 工具 introduce
背景
从Klustron-1.2 版本开始,用户可以直接使用Klustron CDC 组件来完成数据更新日志的流式导出,不再需要本文所述的Binlog2sync工具。
Klustron 为分布式关系数据库管理系统,面向 TB 和 PB 级别海量数据处理,以高吞吐量和低延时处理海量数据高并发读写请求。
客户在使用 Klustron 时可能需要将原来 MySQL 系统中数据导入到 Klustron 中,或者把 Klustron 中数据实时同步到其他存储系统中情况。
为了支持这些功能 Klustron 团队开发了 Binlog2sync 工具。
Binlog2sync 特点
通过 Binlogdump 协议连接到源 MySQL 上,dump 源 MySQL上 Binlog事件;或者直接读取 Binlog 文件进行解析。
根据 Binlog 事件类型处理对应事件,将 INSERT/UPDATE/DELETE 等 DML 事件转换成标准 SQL 语句;对于 DDL 事件则直接输出。
对于分布式 XA 事件,则根据 XA 事件 commit/rollback 来决定是否输出 SQL 语句,如果 XA rollback,则不输出 SQL 语句。
为了防止一个 XA 事件包含多条 SQL 语句导致 Binlog2sync 工具占用大量内存,支持用户配置缓存多少条 SQL,当超过配置值时,自动将缓存 SQL 语句写入磁盘。
Binlog2sync 工具支持 binlog_dump 和 binlog_dump_gtid 两种方式 dump binlog,根据用户输出参数来自动判断。
支持 gtid,binlog 文件位置以及时间条件等过滤。另外还支持库表级别过滤和映射等功能。
Binlog2sync 使用前置条件
binlog_format = row。
binlog_row_image = full(建议开启),如果为 minimal 模式,Binlog2sync 也能正常工作,binlog2sync 解析到具体表时,如果没有缓存该表结构,则通过 information_schema. COLUMNS 表,获取表结构的元信息,这个地方要求从 dump binlog 位置开始,不能有表结构变更操作。
远程 dump binlog 时,账号最小权限为 select, replication slave。
Binlog2sync 使用方法
命令行参数说明
binlog2sync:
-h [ --help ] print usage message
--include_dbs arg need parse log event for db, Format: db1,db2,...
--remap_rules arg db.table remap to new db.table, Format: db1.t1=>db2.t2,db11.t11=>db12.t12, ...
--remote_host arg connect remote mysql host
--remote_port arg connect remote mysql port
--remote_user arg connect remote mysql user
--remote_password arg connect remote mysql password
--remote_binlog_file arg start dump binlog from binlog file
--binlog_position arg start dump binlog from binlog position
--exclude_gtids arg sync events but those gtids
--local_binlog_file arg parse local binlog file
--db_host arg send sql to db host
--db_port arg send sql to db port
--db_user arg send sql to db user
--db_password arg send sql to db password
--commit_sql_num arg number of one commit sql(如果不输入,默认为100条)
--reserve_event_dir arg save sql into file directory
--reserve_event_count arg reserve maxinum of sql in meomry
--job_id arg binlog sync job id
--stop_datetime arg stop to parse binlog in date time
--start_datetime arg start to parse binlog in date time
--stop_never_server_id arg assign server id to connect db sync binlog(如果输入该参数则工具随机生成)
--stop_never arg sync binlog forever
--verbose arg print sync sql, default 0
远程连接 mysql dump binlog 事件,下游同步到 mysql/Klustron中。
./binlog2sync –remote_host=127.0.0.1 –remote_port=1000 –remote_user=xxx
–remote_password=xxxx –remote_binlog_file=binlog.xxxxx –binlog_position=xx –db_host=127.0.0.2
–db_port=1001 –db_user=xxx –db_password=xxxx
中间输出 SQL 语句如下图:
dump 本地 binlog 文件,下游同步到 mysql/Klustron 中。
./binlog2sync --local_binlog_file=binlog.000001 –binlog_position=xxx –db_host=127.0.0.2
–db_port=1001 –db_user=xxx –db_password=xxxx
中间输出 SQL 语句如下图: