024、数据库管理之数据同步工具TiCDC

TiCDC

TiCDC介绍

TiCDC 是一款通过拉取 TiKV 变更日志实现的 TiDB 增量数据同步工具,具有将数据还原到与上游任意 TSO 一致状态的能力,同时提供开放数据协议 (TiCDC Open Protocol),支持其他系统订阅数据变更。

架构与原理

在这里插入图片描述
每个Capture: 不是收集所有TiKV当中的日志,只是负责部分TiKV,然后会选择一个Caputre作为owner将所有的日志进行处理排序,然后供给下游系统订阅

适用场景

TiCDC适合上游数据库是TiDB,下游数据库支持Mysql兼容的任何数据库和kafka, 适合异步复制的场景。
有效索引的相关要求

TiCDC 只能同步至少存在一个有效索引的表,有效索引的定义如下:

  • 主键 (PRIMARY KEY) 为有效索引。
  • 同时满足下列条件的唯一索引 (UNIQUE INDEX) 为有效索引:
    • 索引中每一列在表结构中明确定义非空 (NOT NULL)。
    • 索引中不存在虚拟生成列 (VIRTUAL GENERATED COLUMNS)。
      TiCDC 从 4.0.8 版本开始,可通过修改任务配置来同步没有有效索引的表,但在数据一致性的保证上有所减弱。具体使用方法和注意事项参考同步没有有效索引的表。

TiCDC 暂不支持的场景如下:

  • 暂不支持单独使用 RawKV 的 TiKV 集群。
  • 暂不支持在 TiDB 中创建 SEQUENCE 的 DDL 操作和 SEQUENCE 函数。在上游 TiDB 使用 SEQUENCE 时,TiCDC 将会忽略掉上游执行的 SEQUENCE DDL 操作/函数,但是使用 SEQUENCE 函数的 DML 操作可以正确地同步。

对已有TiDB进行扩容部署

TiCDC环境部署
一般分两种情况:可以前期随 TiDB 一起部署,也可以后期进行扩容部署。

编辑扩容配置文件,准备将 TiCDC 节点 192.168.16.13加入到集群中去.

vim scale-out.yaml  
cdc_servers:
  - host: 192.168.75.15
    gc-ttl: 86400
    data_dir: /tidb-data/cdc-data/cdc-8300
  - host: 192.168.75.16
    gc-ttl: 86400
    data_dir: /tidb-data/cdc-data/cdc-8300
cdc_servers 约定了将 TiCDC 服务部署到哪些机器上,同时可以指定每台机器上的服务配置。
gc-ttl:TiCDC 在 PD 设置的服务级别 GC safepoint 的 TTL (Time To Live) 时长,单位为秒,默认值为 86400,即 24 小时。
port:TiCDC 服务的监听端口,默认 8300

加入 2 个 TiCDC 节点,IP 为 192.168.75.15/16,端口默认 8300,软件部署默认在 /tidb-deploy/cdc-8300 中,日志部署在 /tidb-deploy/cdc-8300/log 中,数据目录在 /tidb-data/cdc-data/cdc-8300 中。

使用 tiup 为原有 TiDB 数据库集群扩容 TiCDC 节点。

扫描二维码关注公众号,回复: 15600398 查看本文章
tiup cluster scale-out tidb-test scale-out.yaml -uroot -p

TiCDC管理工具

cdc cli 是指通过cdc binary 执行cli子命令。通过cdc binary 直接执行cli命令,需要带上PD的地址。

TiCDC同步任务

tiup cdc cli changefeed create --pd=http://192.168.16.13:2379 --sink-uri="mysql://root:[email protected]:3306/" --changefeed-id="simple-replication-task"

  • 公共配置
--changefeed-id : 同步任务的ID
-sink-uri : 同步任务下游的地址,需要按照以下格式进行配置,目前支持mysql/tidb/kafka/pulsar
--start-ts: 指定changefeed的开始TSO
--target-ts:  指定changefeed的目标TSO

下游配置(MySQL/TiDB)

  • worker-count: 向下游执行SQL的并发度(可选,默认值为16)
  • max-txn-row: 向下游执行SQL的bath大小(可选,默认值为256)

下游配置(Kafka)

  • protocol: 输出到kafka消息协议,可选有default、cannal、avro、maxwell
  • max-message-bytes: 每次向kafka broker发送消息的最大数量(可选,默认值为64Mb)

查询所有TiCDC同步任务

cdc cli changefeed list --pd=http://192.168.16.13:2379
[root@tidb2 bin]# ./cdc cli changefeed list --pd=http://192.168.16.13:2379
[
  {
    
    
    "id": "replication-task-1",
    "summary": {
    
    
      "state": "normal",
      "tso": 442624753026203649,
      "checkpoint": "2023-07-04 10:03:55.067",
      "error": null
    }
  }
]

checkpoint(TSO):表示已经复制的时间点。

查询TiCDC指定的同步任务

  • 查看概览
-s : summary 概览
[root@tidb2 bin]# ./cdc cli changefeed query -s --pd=http://192.168.16.13:2379 --changefeed-id=replication-task-1
{
    
    
 "state": "normal",
 "tso": 442624745122562049,
 "checkpoint": "2023-07-04 10:03:24.917",
 "error": null
}

  • 查看详情
[root@tidb2 bin]# ./cdc cli changefeed query --pd=http://192.168.16.13:2379 --changefeed-id=replication-task-1
{
    
    
  "info": {
    
    
    "upstream-id": 0,
    "sink-uri": "mysql://dm:[email protected]:3306/",
    "opts": {
    
    },
    "create-time": "2023-07-04T10:00:01.368752155-04:00",
    "start-ts": 442624691740082177,
    "target-ts": 0,
    "admin-job-type": 0,
    "sort-engine": "unified",
    "sort-dir": "",
    "config": {
    
    
      "case-sensitive": true,
      "enable-old-value": true,
      "force-replicate": false,
      "check-gc-safe-point": true,
      "filter": {
    
    
        "rules": [
          "*.*"
        ],
        "ignore-txn-start-ts": null
      },
      "mounter": {
    
    
        "worker-num": 16
      },
      "sink": {
    
    
        "dispatchers": null,
        "protocol": "",
        "column-selectors": null,
        "schema-registry": ""
      },
      "cyclic-replication": {
    
    
        "enable": false,
        "replica-id": 0,
        "filter-replica-ids": null,
        "id-buckets": 0,
        "sync-ddl": false
      },
      "consistent": {
    
    
        "level": "none",
        "max-log-size": 64,
        "flush-interval": 1000,
        "storage": ""
      }
    },
    "state": "normal",
    "error": null,
    "sync-point-enabled": false,
    "sync-point-interval": 600000000000,
    "creator-version": "v6.1.0"
  },
  "status": {
    
    
    "resolved-ts": 442624742750158849,
    "checkpoint-ts": 442624742487752705,
    "admin-job-type": 0
  },
  "count": 0,
  "task-status": []
}

管理TiCDC同步任务

1、停止同步任务

[root@tidb2 bin]# ./cdc cli changefeed pause --pd=http://192.168.16.13:2379 --changefeed-id=replication-task-1

2、恢复同步任务

[root@tidb2 bin]# ./cdc cli changefeed resume  --pd=http://192.168.16.13:2379 --changefeed-id=replication-task-1

2、删除同步任务

[root@tidb2 bin]# ./cdc cli changefeed remove --pd=http://192.168.16.13:2379 --changefeed-id=replication-task-1

动态更新任务

TiCDC从4.0.4开始支持非动态修改同步任务配置。 当然修改同步任务的时候的时候,需要先暂停任务

1、暂停

[root@tidb2 bin]# ./cdc cli changefeed pause -c replication-task-1 --pd=http://192.168.16.13:2379

2、修改

[root@tidb2 bin]# ./cdc cli changefeed update -c replication-task-1 --pd=http://192.168.16.13:2379 --sink-uri="mysql://localhost:3306/?max-txn-row=20&worker-number=8" --config=changefeed.yaml

3、恢复

[root@tidb2 bin]# ./cdc cli changefeed resume -c replication-task-1 --pd=http://192.168.16.13:2379

监控TiCDC

可以使用grafana 当中的监控,查看TiCDC - Changefeed 相关情况

server : TiDB集群中TiKV节点和TiCDC节点的概要信息
changefeed : TiCDC同步任务的详细信息
events : TiCDC 内部数据流转的详细信息。
tikv : TiKV中的TiCDC相关详细信息

实验

数据同步完整实操

1、查看当前集群

[root@tidb2 ~]# tiup cluster display tidb-test
tiup is checking updates for component cluster ...
Starting component `cluster`: /root/.tiup/components/cluster/v1.12.3/tiup-cluster display tidb-test
Cluster type:       tidb
Cluster name:       tidb-test
Cluster version:    v6.1.0
Deploy user:        root
SSH type:           builtin
Dashboard URL:      http://192.168.16.13:2379/dashboard
ID                   Role  Host           Ports        OS/Arch       Status   Data Dir               Deploy Dir
--                   ----  ----           -----        -------       ------   --------               ----------
192.168.16.13:8300   cdc   192.168.16.13  8300         linux/x86_64  Up       /tidb-data/cdc-8300    /tidb-deploy/cdc-8300
192.168.16.13:2379   pd    192.168.16.13  2379/2380    linux/x86_64  Up|L|UI  /tidb-data/pd-2379     /tidb-deploy/pd-2379
192.168.16.13:4000   tidb  192.168.16.13  4000/10080   linux/x86_64  Up       -                      /tidb-deploy/tidb-4000
192.168.16.13:20160  tikv  192.168.16.13  20160/20180  linux/x86_64  Up       /tidb-data/tikv-20160  /tidb-deploy/tikv-20160
Total nodes: 4

up 表示安装成功

2、查看TiCDC节点信息

[root@tidb2 ~]# tiup ctl:v6.1.0 cdc capture list --pd=http://192.168.16.13:2379
Starting component `ctl`: /root/.tiup/components/ctl/v6.1.0/ctl cdc capture list --pd=http://192.168.16.13:2379
[
  {
    
    
    "id": "1713c29a-6591-40b1-b624-3bccae110eb4",
    "is-owner": true,
    "address": "192.168.16.13:8300"
  }
]

# pd可以是任意节点
# is-owner : true 表示当前节点TiCDC的owner节点

3、数据准备
目的: 上游插入数据后,下游能同步接收

  • TiDB端(上游)
mysql> select * from sales;
Empty set (0.00 sec)
  • mysql端(下游)
mysql> select * from sales;
Empty set (0.00 sec)

4、开启数据同步任务
进入刚刚部署的TiCDC节点,开启数据同步任务

[root@tidb2 bin]# ./cdc cli changefeed create --pd=http://192.168.16.13:2379 --sink-uri="mysql://dm:[email protected]:3306/" --changefeed-id="replication-task-1" --sort-engine="unified"Create changefeed successfully!
ID: replication-task-1
Info: {
    
    "upstream-id":0,"sink-uri":"mysql://dm:[email protected]:3306/","opts":{
    
    },"create-time":"2023-07-04T10:00:01.368752155-04:00","start-ts":442624691740082177,"target-ts":0,"admin-job-type":0,"sort-engine":"unified","sort-dir":"","config":{
    
    "case-sensitive":true,"enable-old-value":true,"force-replicate":false,"check-gc-safe-point":true,"filter":{
    
    "rules":["*.*"],"ignore-txn-start-ts":null},"mounter":{
    
    "worker-num":16},"sink":{
    
    "dispatchers":null,"protocol":"","column-selectors":null,"schema-registry":""},"cyclic-replication":{
    
    "enable":false,"replica-id":0,"filter-replica-ids":null,"id-buckets":0,"sync-ddl":false},"consistent":{
    
    "level":"none","max-log-size":64,"flush-interval":1000,"storage":""}},"state":"normal","error":null,"sync-point-enabled":false,"sync-point-interval":600000000000,"creator-version":"v6.1.0"}

--sink-uri="mysql://dm:[email protected]:3306" : 下游mysql用户密码 数据库地址和端口 
--changefeed-id="replication-task-1": 任务ID
--sort-engine="unified" : 不指定引擎

5、查询任务

[root@tidb2 bin]# ./cdc cli changefeed list --pd=http://192.168.16.13:2379
[
  {
    
    
    "id": "replication-task-1",
    "summary": {
    
    
      "state": "normal",  # state: normal 表示任务状态正常
      "tso": 442624473383829505,  # 表示当前任务的时间戳信息
      "checkpoint": "2023-07-04 09:46:08.316",  # 表示当前同步任务的时间
      "error": null
    }
  }
]

6、查看复制任务的详细信息

[root@tidb2 bin]# ./cdc cli changefeed query --pd=http://192.168.16.13:2379 --changefeed-id=replication-task-1
{
    
    
  "info": {
    
    
    "upstream-id": 0,
    "sink-uri": "mysql://dm:[email protected]:3306/",
    "opts": {
    
    },
    "create-time": "2023-07-04T10:00:01.368752155-04:00",
    "start-ts": 442624691740082177,
    "target-ts": 0,
    "admin-job-type": 0,
    "sort-engine": "unified",
    "sort-dir": "",
    "config": {
    
    
      "case-sensitive": true,
      "enable-old-value": true,
      "force-replicate": false,
      "check-gc-safe-point": true,
      "filter": {
    
    
        "rules": [
          "*.*"
        ],
        "ignore-txn-start-ts": null
      },
      "mounter": {
    
    
        "worker-num": 16
      },
      "sink": {
    
    
        "dispatchers": null,
        "protocol": "",
        "column-selectors": null,
        "schema-registry": ""
      },
      "cyclic-replication": {
    
    
        "enable": false,
        "replica-id": 0,
        "filter-replica-ids": null,
        "id-buckets": 0,
        "sync-ddl": false
      },
      "consistent": {
    
    
        "level": "none",
        "max-log-size": 64,
        "flush-interval": 1000,
        "storage": ""
      }
    },
    "state": "normal",
    "error": null,
    "sync-point-enabled": false,
    "sync-point-interval": 600000000000,
    "creator-version": "v6.1.0"
  },
  "status": {
    
    
    "resolved-ts": 442624718698184705,
    "checkpoint-ts": 442624718436302849,
    "admin-job-type": 0
  },
  "count": 0,
  "task-status": []
}

7、数据验证

mysql> select * from sales;
+----+-------+------+
| id | pname | cnt  |
+----+-------+------+
|  1 | xx    |  100 |
+----+-------+------+
1 row in set (0.01 sec)

缩容当前TiCDC节点

1、停止同步任务

[root@tidb2 bin]# ./cdc cli changefeed pause --pd=http://192.168.16.13:2379 --changefeed-id=replication-task-1

2、删除同步任务

[root@tidb2 bin]# ./cdc cli changefeed remove --pd=http://192.168.16.13:2379 --changefeed-id=replication-task-1

3 缩容当前TiCDC节点

tiup cluster scale-in tidb-test --node=192.168.16.13:8300

猜你喜欢

转载自blog.csdn.net/wangzhicheng987/article/details/131049750