TiDB Binlog工具Drainer使用

Posted 清风明月li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TiDB Binlog工具Drainer使用相关的知识,希望对你有一定的参考价值。

一、TiDB Binlog简介
TiDB Binlog是一个用于收集TiDB的binlog,并提供准实时备份和同步功能的商业化工具。
TiDB Blinlog支持以下功能场景:
• 数据同步:同步 TiDB 集群数据到其他数据库
• 实时备份和恢复:备份 TiDB 集群数据,同时可以用于 TiDB 集群故障时恢复

注意
TiDB Binlog 与 TiDB v5.0 版本开始引入的一些特性不兼容,无法一起使用,详情参照注意事项。建议使用 TiCDC 替代 TiDB Binlog。

二、TiDB Binlog整体架构

2.1、组件的功能
TiDB Binlog 集群主要分为 Pump 和 Drainer 两个组件,以及 binlogctl 工具:
Pump
Pump 用于实时记录 TiDB 产生的 Binlog,并将 Binlog 按照事务的提交时间进行排序,再提供给 Drainer 进行消费。
Drainer
Drainer 从各个 Pump 中收集 Binlog 进行归并,再将 Binlog 转化成 SQL 或者指定格式的数据,最终同步到下游。
binlogctl 工具
binlogctl 是一个 TiDB Binlog 配套的运维工具,具有如下功能:
• 获取 TiDB 集群当前的 TSO
• 查看 Pump/Drainer 状态
• 修改 Pump/Drainer 状态
• 暂停/下线 Pump/Drainer
2.2、主要特性
• 多个 Pump 形成一个集群,可以水平扩容。
• TiDB 通过内置的 Pump Client 将 Binlog 分发到各个 Pump。
• Pump 负责存储 Binlog,并将 Binlog 按顺序提供给 Drainer。
• Drainer 负责读取各个 Pump 的 Binlog,归并排序后发送到下游。
• Drainer 支持 relay log 功能,通过 relay log 保证下游集群的一致性状态。
2.3、注意事项
• TiDB Binlog 和 TiDB 在 v5.1 版本中解决了 v5.0 版本中引入的聚簇索引与 TiDB Binlog 不兼容问题。在升级 TiDB Binlog 和 TiDB Server 到 v5.1 版本后:开启 TiDB Binlog 后,TiDB 支持创建聚簇索引表;聚簇索引表的数据插入、删除和更新操作支持通过 TiDB Binlog 同步到下游。对于同步聚簇索引表时需注意:
○ 如果从 v5.0 版本手动控制组件升级顺序进行升级,请确保先将 TiDB Binlog 升级至 v5.1 版本后再将 TiDB Server 升级至 v5.1 版本。
○ 推荐将上下游的 TiDB 系统变量 tidb_enable_clustered_index 配置为一致的值来保证上下游 TiDB 聚簇索引表结构一致。
• TiDB Binlog 与 TiDB v5.0 版本开始引入的以下特性不兼容,无法一起使用:
○ TiDB 聚簇索引特性:开启 TiDB Binlog 后 TiDB 不允许创建非单个整数列作为主键的聚簇索引;已创建的聚簇索引表的数据插入、删除和更新动作不会通过 TiDB Binlog 同步到下游。如需同步聚簇索引表,请升级至 v5.1 版本或使用 TiCDC;
○ TiDB 系统变量 tidb_enable_async_commit:启用 TiDB Binlog 后,开启该选项无法获得性能提升。要获得性能提升,建议使用 TiCDC 替代 TiDB Binlog。
○ TiDB 系统变量 tidb_enable_1pc:启用 TiDB Binlog 后,开启该选项无法获得性能提升。要获得性能提升,建议使用 TiCDC 替代 TiDB Binlog。
• TiDB Binlog 与 TiDB v4.0.7 版本开始引入的以下特性不兼容,无法一起使用:
○ TiDB 系统变量 tidb_enable_amend_pessimistic_txn:两个功能存在兼容性问题,一起使用会造成 TiDB Binlog 复制数据不一致的正确性问题。
• Drainer 支持将 Binlog 同步到 mysql、TiDB、Kafka 或者本地文件。如果需要将 Binlog 同步到其他 Drainer 不支持的类型的系统中,可以设置 Drainer 将 Binlog 同步到 Kafka,然后根据 binlog consumer protocol 进行定制处理,参考 Binlog Consumer Client 用户文档。
• 如果 TiDB Binlog 用于增量恢复,可以设置配置项 db-type="file",Drainer 会将 binlog 转化为指定的 proto buffer 格式的数据,再写入到本地文件中。这样就可以使用 Reparo 恢复增量数据。
关于 db-type 的取值,应注意:
○ 如果 TiDB 版本 < 2.1.9,则 db-type="pb"。
○ 如果 TiDB 版本 > = 2.1.9,则 db-type="file" 或 db-type="pb"。
• 如果下游为 MySQL/TiDB,数据同步后可以使用 sync-diff-inspector 进行数据校验。
三、TiDB Binlog数据同步到mysql
3.1、环境架构图

序号 IP 角色 备注
1 172.16.5.122:2379 PD(leader)
2 172.16.5.122:4000 tidb
3 172.16.5.122:4000 tikv
4 172.16.6.151:20160 tikv
5 172.16.6.236:20160 tikv
6 172.16.5.146:8249 drainer
7 172.16.5.122:8250 pump
8 172.16.6.130:3306 Mysql server
3.2、配置下游的mysql
下游的mysql数据库地址为172.16.6.130:3306

CoffeeScript
mysql> create user root@% identified by mysql;
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on . to root@%;

#备注,相关的配置文件
[root@k8s-master ~]# cat drainer_scale-out.toml
drainer_servers:

  • host: 172.16.5.146
    commit_ts: -1
    config:
    syncer.db-type: "mysql"
    syncer.to.host: "172.16.6.130"
    syncer.to.user: "root"
    syncer.to.password: "mysql"
    syncer.to.port: 3306
    syncer.replicate-do-db: ["testlqb"]
    3.3、配置上游的Tidb数据库
    1、登陆tidb数据库,在testlqb数据库中创建表T,为验证数据做准备

C#
MySQL [(none)]> create database testlqb;

Query OK, 0 rows affected (0.12 sec)

MySQL [(none)]>
MySQL [(none)]> use testlqb;
Database changed
MySQL [testlqb]> create table T(id int primary key, name varchar(30))
-> ;
Query OK, 0 rows affected (0.13 sec)

MySQL [testlqb]> insert into T values(1,tom);
Query OK, 1 row affected (0.01 sec)

MySQL [testlqb]> insert into T values(2,Jack);
Query OK, 1 row affected (0.01 sec)

MySQL [testlqb]> insert into T values(3,Frank);
Query OK, 1 row affected (0.01 sec)

MySQL [testlqb]> select * from T;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | Jack |
| 3 | Frank |
+----+-------+
3 rows in set (0.01 sec)
2、登陆下游的mysql,在lqb数据库中创建表T

C#
mysql> create database lqb;
Query OK, 1 row affected (0.00 sec)

mysql> create table T(id int primary key, name varchar(30))
-> ;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into T values(1,tom);
Query OK, 1 row affected (0.00 sec)

mysql> insert into T values(3,Frank);
Query OK, 1 row affected (0.00 sec)

mysql> insert into T values(2,Jack);
Query OK, 1 row affected (0.00 sec)

mysql> select * from T;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | Jack |
| 3 | Frank |
+----+-------+
3 rows in set (0.00 sec)
3.4、安装Drainer和pump
drainer的安装有两种方式,一种是tiup,另一种是Binary 部署 TiDB Binlog。推荐使用tiup。扩容出pump节点和drainer节点为后边数据同步复制做准备。
1、查看tiup集群的状况,tidb安装略

Haskell
[root@k8s-master ~]# tiup cluster display tidb01
tiup is checking updates for component cluster ...
Starting component cluster: /root/.tiup/components/cluster/v1.9.5/tiup-cluster /root/.tiup/components/cluster/v1.9.5/tiup-cluster display tidb01
Cluster type: tidb
Cluster name: tidb01
Cluster version: v4.0.12
Deploy user: tidb
SSH type: builtin
Dashboard URL: http://172.16.5.146:2379/dashboard
ID Role Host Ports OS/Arch Status Data Dir Deploy Dir


172.16.5.122:9093 alertmanager 172.16.5.122 9093/9094 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/alertmanager-9093 /home/tidb/tidb/v4.0.12/tidb-deploy/alertmanager-9093
172.16.5.122:3000 grafana 172.16.5.122 3000 linux/x86_64 Up - /home/tidb/tidb/v4.0.12/tidb-deploy/grafana-3000
172.16.5.122:2379 pd 172.16.5.122 2379/2380 linux/x86_64 Up|L /home/tidb/tidb/v4.0.12/tidb-data/pd-2379 /home/tidb/tidb/v4.0.12/tidb-deploy/pd-2379
172.16.5.146:2379 pd 172.16.5.146 2379/2380 linux/x86_64 Up|UI /home/tidb/tidb/v4.0.12/tidb-data/pd-2379 /home/tidb/tidb/v4.0.12/tidb-deploy/pd-2379
172.16.6.236:2379 pd 172.16.6.236 2379/2380 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/pd-2379 /home/tidb/tidb/v4.0.12/tidb-deploy/pd-2379
172.16.5.122:9090 prometheus 172.16.5.122 9090 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/prometheus-9090 /home/tidb/tidb/v4.0.12/tidb-deploy/prometheus-9090
172.16.5.122:4000 tidb 172.16.5.122 4000/10080 linux/x86_64 Up - /home/tidb/tidb/v4.0.12/tidb-deploy/tidb-4000
172.16.5.146:20160 tikv 172.16.5.146 20160/20180 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/tikv-20160 /home/tidb/tidb/v4.0.12/tidb-deploy/tikv-20160
172.16.6.151:20160 tikv 172.16.6.151 20160/20189 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/tikv-20160 /home/tidb/tidb/v4.0.12/tidb-deploy/tikv-20160
172.16.6.236:20160 tikv 172.16.6.236 20160/20180 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/tikv-20160 /home/tidb/tidb/v4.0.12/tidb-deploy/tikv-20160
2、创建扩展文件

CSS
[root@k8s-master ~]# cat drainer_scale-out.toml
drainer_servers:

  • host: 172.16.5.146
    commit_ts: -1
    config:
    syncer.db-type: "mysql"
    syncer.to.host: "172.16.6.130"
    syncer.to.user: "root"
    syncer.to.password: "mysql"
    syncer.to.port: 3306
    syncer.replicate-do-db: ["testlqb"]
    3、扩展drainer

SQL
1、检查配置文件
tiup cluster check tidb01 drainer_scale-out.toml --cluster --apply --user root -i /root/.ssh/id_rsa
2、扩展drainer
tiup cluster scale-out tidb01 drainer_scale-out.toml -i /root/.ssh/id_rsa
3、查看是否成功
[root@k8s-master ~]# tiup cluster display tidb01
tiup is checking updates for component cluster ...
Starting component cluster: /root/.tiup/components/cluster/v1.9.5/tiup-cluster /root/.tiup/components/cluster/v1.9.5/tiup-cluster display tidb01
Cluster type: tidb
Cluster name: tidb01
Cluster version: v4.0.12
Deploy user: tidb
SSH type: builtin
Dashboard URL: http://172.16.5.146:2379/dashboard
ID Role Host Ports OS/Arch Status Data Dir Deploy Dir


172.16.5.122:9093 alertmanager 172.16.5.122 9093/9094 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/alertmanager-9093 /home/tidb/tidb/v4.0.12/tidb-deploy/alertmanager-9093
172.16.5.146:8249 drainer 172.16.5.146 8249 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/drainer-8249 /home/tidb/tidb/v4.0.12/tidb-deploy/drainer-8249
4、通过tiup对drainer进行管理

Gherkin
1、关闭drainer
[root@k8s-master ~]# tiup cluster stop tidb01 -N 172.16.5.146:8249
2、启动drainer
[root@k8s-master ~]# tiup cluster start tidb01 -N 172.16.5.146:8249
3、对数据库进行查询
MySQL [testlqb]> show drainer status;
+-------------------+-------------------+--------+--------------------+---------------------+
| NodeID | Address | State | Max_Commit_Ts | Update_Time |
+-------------------+-------------------+--------+--------------------+---------------------+
| 172.16.5.146:8249 | 172.16.5.146:8249 | paused | 433333952724074497 | 2022-05-20 17:10:47 |
+-------------------+-------------------+--------+--------------------+---------------------+
1 row in set (0.02 sec)

MySQL [testlqb]> show drainer status;
+-------------------+-------------------+--------+--------------------+---------------------+
| NodeID | Address | State | Max_Commit_Ts | Update_Time |
+-------------------+-------------------+--------+--------------------+---------------------+
| 172.16.5.146:8249 | 172.16.5.146:8249 | online | 433333952724074497 | 2022-05-20 17:11:18 |
+-------------------+-------------------+--------+--------------------+---------------------+
注意事项

  1. drainer重启后,会从上次复制的断点,继续同步日志。
    复制TSO信息保存在目标库的tidb_binlog_checkpoint表中。
  2. drainer启动后的5分钟内,自动启用safe-mode模式。
    会用 replace 替换 insert 语句,用 delete + replace 替换 update 语句。
    5、安装pump,步骤和drainer一样

YAML
1创建pump扩展文件
[root@k8s-master ~]# cat pump_scale-out.toml
pump_servers:

  • host: 172.16.5.122
    ssh_port: 22
    port: 8250
    deploy_dir: /home/tidb/tidb/v4.0.12/tidb-deploy/pump-8250
    data_dir: /home/tidb/tidb/v4.0.12/tidb-data/pump-8250
    log_dir: /home/tidb/tidb/v4.0.12/tidb-deploy/pump-8250/log
    arch: amd64
    os: linux

2、检查配置
[root@k8s-master ~]# tiup cluster check tidb01 pump_scale-out.toml --cluster --apply --user root -i /root/.ssh/id_rsa
3、安装
[root@k8s-master ~]# tiup cluster scale-out tidb01 pump_scale-out.toml -i /root/.ssh/id_rsa
4、检查集群
[root@k8s-master ~]# tiup cluster display tidb01
ID Role Host Ports OS/Arch Status Data Dir Deploy Dir


172.16.5.122:8250 pump 172.16.5.122 8250 linux/x86_64 Up /home/tidb/tidb/v4.0.12/tidb-data/pump-8250 /home/tidb/tidb/v4.0.12/tidb-deploy/pump-8250
3.5、配置和验证
1、启动pump节点和drainer节点后,需要开启Tidb数据库的binlog日志。
开启binlog日志可以使用tiup cluster edit-config命令编辑系统变量binlog.enable:true和binlog.ignore-error: true。

YAML
[root@k8s-master ~]# tiup cluster edit-config tidb01

server_configs:
tidb:
binlog.enable: true
binlog.ignore-error: true
2、重新加载改配置文件

C#
tiup cluster edit-config tidb01
3、登陆tidb进行验证和查看pump和drainer状态

Gherkin
MySQL [mysql]> show variables like "log_bin";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | 1 |
+---------------+-------+
1 row in set (0.01 sec)
MySQL [mysql]> show pump status;
+-------------------+-------------------+--------+--------------------+---------------------+
| NodeID | Address | State | Max_Commit_Ts | Update_Time |
+-------------------+-------------------+--------+--------------------+---------------------+
| 172.16.5.122:8250 | 172.16.5.122:8250 | online | 433329819276804097 | 2022-05-20 12:47:57 |
+-------------------+-------------------+--------+--------------------+---------------------+
1 row in set (0.01 sec)

MySQL [mysql]> show drainer status;
+-------------------+-------------------+--------+--------------------+---------------------+
| NodeID | Address | State | Max_Commit_Ts | Update_Time |
+-------------------+-------------------+--------+--------------------+---------------------+
| 172.16.5.146:8249 | 172.16.5.146:8249 | online | 433329821636100097 | 2022-05-20 12:48:06 |
+-------------------+-------------------+--------+--------------------+---------------------+
1 row in set (0.00 sec)
从上边的查询会看到pump和drainer的状态为online
3.6、同步复制tidb数据库到mysql
1、登陆tidb数据库,查询test数据库的表T

Gherkin
MySQL [(none)]> use testlqb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [testlqb]> select * from T;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | Jack |
| 3 | Frank |
+----+-------+
2、登陆msyql数据库查询该表

Gherkin
MySQL [testlqb]> select * from T;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | Jack |
| 3 | Frank |
+----+-------+
3 rows in set (0.00 sec)
3、登陆tidb数据库,并插入数据

Gherkin
MySQL [testlqb]> insert into T select 4,tony;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

MySQL [testlqb]> select * from T;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | Jack |
| 3 | Frank |
| 4 | tony |
+----+-------+
4 rows in set (0.00 sec)

MySQL [testlqb]> insert into T select 5,lqb;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
4、登陆mysql数据库查看数据同步

Gherkin
MySQL [testlqb]> select * from T;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | Jack |
| 3 | Frank |
| 4 | tony |
| 5 | lqb |
+----+-------+
5 rows in set (0.00 sec)
备注,在源端从其他数据库复制一张表t1,在目的端mysql也能正常查询。数据也已经同步

Gherkin
MySQL [testlqb]> show tables;
+-------------------+
| Tables_in_testlqb |
+-------------------+
| T |
| t1 |
+-------------------+
2 rows in set (0.00 sec)
备注:drainer通过tiup停止和查询

Apache
tiup cluster stop tidb01 -N 172.16.5.146:8249
tiup cluster start tidb01 -N 172.16.5.146:8249
四、通过binlogctl工具进行管理
1、简介
Binlog Control(以下简称 binlogctl)是 TiDB Binlog 的命令行工具,用于管理 TiDB Binlog 集群。
binlogctl 支持如下这些功能:
• 查看 Pump/Drainer 状态
• 暂停/下线 Pump/Drainer
• Pump/Drainer 异常状态处理
使用 binlogctl 的场景:
• 同步出现故障/检查运行情况,需要查看 Pump/Drainer 的状态
• 维护集群,需要暂停/下线 Pump/Drainer
• Pump/Drainer 异常退出,状态没有更新,或者状态不符合预期,对业务造成影响
2、下载

Groovy
wget https://download.pingcap.org/tidb-version-linux-amd64.tar.gz &&
wget https://download.pingcap.org/tidb-version-linux-amd64.sha256
3、使用

Prolog
1、查看pump和drainer状态
[root@k8s-master bin]# ./binlogctl -pd-urls=http://172.16.5.122:2379 -cmd drainers
[2022/05/20 17:18:31.286 +08:00] [INFO] [nodes.go:53] ["query node"] [type=drainer] [node="NodeID: 172.16.5.146:8249, Addr: 172.16.5.146:8249, State: online, MaxCommitTS: 433334074686570497, UpdateTime: 2022-05-20 17:18:29 +0800 CST"]
[root@k8s-master bin]# ./binlogctl -pd-urls=http://172.16.5.122:2379 -cmd pumps
[2022/05/20 17:18:37.281 +08:00] [INFO] [nodes.go:53] ["query node"] [type=pump] [node="NodeID: 172.16.5.122:8250, Addr: 172.16.5.122:8250, State: online, MaxCommitTS: 433334076259434497, UpdateTime: 2022-05-20 17:18:36 +0800 CST"]

2、暂停pump和drainer
[root@k8s-master bin]# ./binlogctl -pd-urls=http://172.16.5.122:2379 -cmd pause-drainer -node-id 172.16.5.146:8249
[2022/05/20 17:21:06.069 +08:00] [INFO] [nodes.go:123] ["Apply action on node success"] [action=pause] [NodeID=172.16.5.146:8249]
[root@k8s-master bin]# ./binlogctl -pd-urls=http://172.16.5.122:2379 -cmd drainers
[2022/05/20 17:21:21.295 +08:00] [INFO] [nodes.go:53] ["query node"] [type=drainer] [node="NodeID: 172.16.5.146:8249, Addr: 172.16.5.146:8249, State: paused, MaxCommitTS: 433334115607248897, UpdateTime: 2022-05-20 17:21:05 +0800 CST"]

暂停drainer的时候下游mysql数据不同步

通过tiup cluster start tidb01 -N 172.16.5.146:8249启动drainer,发现下游数据库同步了

cmd 说明 示例
pause-pump 暂停 Pump bin/binlogctl -pd-urls=http://127.0.0.1:2379 -cmd pause-pump -node-id ip-127-0-0-1:8250
pause-drainer 暂停 Drainer bin/binlogctl -pd-urls=http://127.0.0.1:2379 -cmd pause-drainer -node-id ip-127-0-0-1:8249
offline-pump 下线 Pump bin/binlogctl -pd-urls=http://127.0.0.1:2379 -cmd offline-pump -node-id ip-127-0-0-1:8250
offline-drainer 下线 Drainer bin/binlogctl -pd-urls=http://127.0.0.1:2379 -cmd offline-drainer -node-id ip-127-0-0-1:8249

以上是关于TiDB Binlog工具Drainer使用的主要内容,如果未能解决你的问题,请参考以下文章

TIDB - 使用 TiDB Binlog 实现数据复制

TIDB - 使用 TICDC 将数据同步至下游 Mysql 中

tidb同步mysql

TiDB Online DDL 在 TiCDC 中的应用丨TiDB 工具分享

TIDB - 使用 TICDC 将数据同步至下游 Kafka 中

TIDB - 使用 TICDC 将数据同步至下游 Kafka 中