写点什么

TiDB Binlog 功能使用实践

  • 2024-03-08
    北京
  • 本文字数:4748 字

    阅读完需:约 16 分钟

作者: paulli 原文来源:https://tidb.net/blog/cc2c7aa5

一、TiDB Binlog 功能

TiDB Binlog 功能

TiDB binlog 提供准实时备份和同步功能,支持以下功能场景:


  • 数据同步:同步 TiDB 集群数据到其他数据库

  • 实时备份和恢复:备份 TiDB 集群数据,同时可以用于 TiDB 集群故障时恢复


TiDB Binlog 集群主要分为 Pump 和 Drainer 两个组件,以及 binlogctl 工具:


Pump,用于实时记录 TiDB 产生的 Binlog,并将 Binlog 按照事务的提交时间进行排序,再提供给 Drainer 进行消费。


Drainer,从各个 Pump 中收集 Binlog 进行归并,再将 Binlog 转化成 SQL 或者指定格式的数据,最终同步到下游。


binlogctl 工具,获取 TiDB 集群当前的 TSO,查看 / 修改 / 下线 Pump/Drainer 状态。

如何开启 TiDB Binlog

[tidb@tidb53 paul]$ tiup cluster edit-config paul_tserver_configs:tidb:  binlog.enable: true   binlog.ignore-error: true
复制代码

二、部署 TiDB Binlog 集群

场景一:部署 TiDB binlog 集群,drainer 写入 mysql 数据库

[tidb@tidb53 paul]$ cat scale-out-binlog.tomlpump_servers:- host: 172.20.12.52ssh_port: 22port: 8250deploy_dir: /data2/tidb-deploy/pump-8250data_dir: /data2/tidb-data/pump-8250log_dir: /data2/tidb-deploy/pump-8250/logarch: amd64os: linuxdrainer_servers:- host: 172.20.12.70ssh_port: 22port: 8249deploy_dir: /data2/tidb-deploy/drainer-8249data_dir: /data2/tidb-data/drainer-8249log_dir: /data2/tidb-deploy/drainer-8249/logconfig:  syncer.db-type: mysql  syncer.relay.log-dir: /data2/tidb-data/drainer-8249  syncer.relay.max-file-size: 10485760  syncer.replicate-do-table:  - db-name: test    tbl-name: t1  syncer.to.checkpoint.host: 172.20.12.52  syncer.to.checkpoint.password: root  syncer.to.checkpoint.port: 3900  syncer.to.checkpoint.schema: tidb_binlog  syncer.to.checkpoint.table: checkpoint  syncer.to.checkpoint.type: mysql  syncer.to.checkpoint.user: root  syncer.to.host: 172.20.12.52  syncer.to.password: root  syncer.to.port: 3900  syncer.to.user: rootarch: amd64os: linux  [tidb@tidb53 paul]$ tiup cluster scale-out paul_t scale-out-binlog.toml tiup is checking updates for component cluster ...Starting component `cluster`: /home/tidb/.tiup/components/cluster/v1.14.1/tiup-cluster scale-out paul_t scale-out-binlog.tomlPlease confirm your topology:Cluster type:   tidbCluster name:   paul_tCluster version: v6.5.1Role     Host         Ports OS/Arch       Directories----     ----         ----- -------       -----------pump     172.20.12.52 8250   linux/x86_64 /data2/tidb-deploy/pump-8250,/data2/tidb-data/pump-8250drainer 172.20.12.70 8249   linux/x86_64 /data2/tidb-deploy/drainer-8249,/data2/tidb-data/drainer-8249Attention:  1. If the topology is not what you expected, check your yaml file.  2. Please confirm there is no port/directory conflicts in same host.Do you want to continue? [y/N]: (default=N) y
复制代码

场景二:部署 TiDB binlog 集群,drainer 写入 file

[tidb@tidb53 paul]$ cat scale-out-binlogfile.toml drainer_servers:- host: 172.20.12.70ssh_port: 22port: 8249deploy_dir: /data2/tidb-deploy/drainer-8249data_dir: /data2/tidb-data/drainer-8249log_dir: /data2/tidb-deploy/drainer-8249/logconfig:  syncer.db-type: file   syncer.to.retention-time: 15  syncer.relay.log-dir: /data2/tidb-data/drainer-8249  syncer.relay.max-file-size: 10485760arch: amd64os: linux [tidb@tidb53 paul]$ tiup cluster scale-out paul_t scale-out-binlogfile.toml tiup is checking updates for component cluster ...Starting component `cluster`: /home/tidb/.tiup/components/cluster/v1.14.1/tiup-cluster scale-out paul_t scale-out-binlogfile.tomlPlease confirm your topology:Cluster type:   tidbCluster name:   paul_tCluster version: v6.5.1Role     Host         Ports OS/Arch       Directories----     ----         ----- -------       -----------drainer 172.20.12.70 8249   linux/x86_64 /data2/tidb-deploy/drainer-8249,/data2/tidb-data/drainer-8249Attention:  1. If the topology is not what you expected, check your yaml file.  2. Please confirm there is no port/directory conflicts in same host.Do you want to continue? [y/N]: (default=N) y 
复制代码

三、运维 TiDB Binlog 集群

查看 drainer

[tidb@tidb53 paul]$ binlogctl -pd-urls=http://172.20.12.70:2881 -cmd drainers --node-id=172.20.12.70:8249[INFO] [nodes.go:53] ["query node"] [type=drainer] [node="{NodeID: 172.20.12.70:8249, Addr: 172.20.12.70:8249, State: online, MaxCommitTS: 448124127741739009, UpdateTime: 2024-03-03 17:24:46 +0800 CST}"]
复制代码

暂停 / 下线 drainer

[tidb@tidb53 paul]$ binlogctl -pd-urls=http://172.20.12.70:2881 -cmd pause-drainer --node-id=172.20.12.70:8249[INFO] [nodes.go:123] ["Apply action on node success"] [action=pause] [NodeID=172.20.12.70:8249]
复制代码

删除 pump 和 drainer 节点

//sacale-in pump[tidb@tidb53 paul]$ tiup cluster scale-in paul_t -N 172.20.12.52:8250 tiup is checking updates for component cluster ...Starting component `cluster`: /home/tidb/.tiup/components/cluster/v1.14.1/tiup-cluster scale-in paul_t -N 172.20.12.52:8250This operation will delete the 172.20.12.52:8250 nodes in `paul_t` and all their data.Do you want to continue? [y/N]:(default=N) yThe component `[pump]` will become tombstone, maybe exists in several minutes or hours, after that you can use the prune command to clean itDo you want to continue? [y/N]:(default=N) yScale-in nodes...//scale-in drainer[tidb@tidb53 paul]$ tiup cluster scale-in paul_t -N 172.20.12.70:8249tiup is checking updates for component cluster ...Starting component `cluster`: /home/tidb/.tiup/components/cluster/v1.14.1/tiup-cluster scale-in paul_t -N 172.20.12.70:8249This operation will delete the 172.20.12.70:8249 nodes in `paul_t` and all their data.Do you want to continue? [y/N]:(default=N) yThe component `[drainer]` will become tombstone, maybe exists in several minutes or hours, after that you can use the prune command to clean itDo you want to continue? [y/N]:(default=N) yScale-in nodes...
复制代码

四、常规测试

DML 操作

上游写入数据mysql> INSERT INTO t1 SELECT NULL, RANDOM_BYTES(1024), RANDOM_BYTES(1024), RANDOM_BYTES(1024),null FROM t1 a JOIN t1 b JOIN t1 c LIMIT 10000;Query OK, 10000 rows affected (0.80 sec)Records: 10000 Duplicates: 0 Warnings: 0drainer日志:[INFO] [pump.go:168] ["receive big size binlog before unmarshal"] [id=172.20.12.52:8250] [size="31 MB"] ["start ts"=448123549203300353] ["commit ts"=448123549360586755][INFO] [binlogger.go:487] ["segmented binlog file is created"] [path=/data2/tidb-data/drainer-8249/binlog-0000000000000008-20240303164758][INFO] [binlogger.go:378] ["GC binlog file"] ["file name"=/data2/tidb-data/drainer-8249/binlog-0000000000000006-20240303164324][INFO] [syncer.go:279] ["write save point"] [ts=448123549360586755] [version=273]
复制代码

DDL 操作

增加字段:


上游tidb添加表t1 字段 mysql> alter table t1 add column nt int;Query OK, 0 rows affected (0.52 sec)drainer日志:[[INFO] [collector.go:285] ["start query job"] [id=270] [binlog="tp:Commit start_ts:448100511433097220 commit_ts:448100511433097225 prewrite_key:\"mDB:85\\000\\000\\000\\374\\000\\000\\000\\000\\000\\000\\000hTable:26\\3775\\000\\000\\000\\000\\000\\000\\000\\370\" ddl_query:\"ALTER TABLE `t1` ADD COLUMN `n1` INT;\" ddl_job_id:270 ddl_schema_state:5 "][INFO] [collector.go:307] ["get ddl job"] [job="ID:270, Type:add column, State:synced, SchemaState:public, SchemaID:85, TableID:265, RowCount:0, ArgLen:0, start time: 2024-03-02 16:23:15.439 +0800 CST, Err:<nil>, ErrCount:0, SnapshotVersion:0"][INFO] [schema.go:315] ["skip unsupported DDL job"] [job="ID:239, Type:alter table cache, State:synced, SchemaState:public, SchemaID:85, TableID:237, RowCount:0, ArgLen:0, start time: 2024-02-23 17:38:28.539 +0800 CST, Err:<nil>, ErrCount:0, SnapshotVersion:0"][INFO] [syncer.go:524] ["add ddl item to syncer, you can add this commit ts to `ignore-txn-commit-ts` to skip this ddl if needed"] [sql="alter table t1 add column n1 int"] ["commit ts"=448100511433097225] [shouldSkip=false][2024/03/02 16:23:18.632 +08:00] [INFO] [load.go:451] ["exec ddl success"] [sql="ALTER TABLE `t1` ADD COLUMN `n1` INT;"][2024/03/02 16:23:18.640 +08:00] [INFO] [binlogger.go:378] ["GC binlog file"] ["file name"=/data2/tidb-data/drainer-8249/binlog-0000000000000002-20240301161117]
复制代码


删除字段


上游tidb添加表t1 字段 mysql> alter table t1 drop column ;Query OK, 0 rows affected (0.52 sec)drainter 日志:[INFO] [collector.go:285] ["start query job"] [id=271] [binlog="tp:Commit start_ts:448100530215714822 commit_ts:448100530215714825 prewrite_key:\"mDB:85\\000\\000\\000\\374\\000\\000\\000\\000\\000\\000\\000hTable:26\\3775\\000\\000\\000\\000\\000\\000\\000\\370\" ddl_query:\"ALTER TABLE `t1` DROP COLUMN `n1`;\" ddl_job_id:271 "][INFO] [collector.go:307] ["get ddl job"] [job="ID:271, Type:drop column, State:synced, SchemaState:none, SchemaID:85, TableID:265, RowCount:0, ArgLen:0, start time: 2024-03-02 16:24:27.089 +0800 CST, Err:<nil>, ErrCount:0, SnapshotVersion:0"][INFO] [schema.go:550] ["Finished dropping column"] [job="ID:271, Type:drop column, State:synced, SchemaState:none, SchemaID:85, TableID:265, RowCount:0, ArgLen:0, start time: 2024-03-02 16:24:27.089 +0800 CST, Err:<nil>, ErrCount:0, SnapshotVersion:0"][INFO] [syncer.go:524] ["add ddl item to syncer, you can add this commit ts to `ignore-txn-commit-ts` to skip this ddl if needed"] [sql="alter table t1 drop column n1"] ["commit ts"=448100530215714825] [shouldSkip=false][INFO] [load.go:451] ["exec ddl success"] [sql="ALTER TABLE `t1` DROP COLUMN `n1`;"][INFO] [syncer.go:279] ["write save point"] [ts=448100530215714825] [version=273][INFO] [syncer.go:279] ["write save point"] [ts=448100533033500673] [version=273]
复制代码


发布于: 刚刚阅读数: 4
用户头像

TiDB 社区官网:https://tidb.net/ 2021-12-15 加入

TiDB 社区干货传送门是由 TiDB 社区中布道师组委会自发组织的 TiDB 社区优质内容对外宣布的栏目,旨在加深 TiDBer 之间的交流和学习。一起构建有爱、互助、共创共建的 TiDB 社区 https://tidb.net/

评论

发布
暂无评论
TiDB Binlog功能使用实践_6.x 实践_TiDB 社区干货传送门_InfoQ写作社区