一、环境配置
主机名 | 服务器地址 |
---|---|
tidb1 | 172.16.22.41 |
tidb2 | 172.16.22.42 |
背景:两台虚拟机服务器本地均装有单机版的TIDB v8.1.0,集群名称为tidb-test
# tiup cluster list
Name User Version Path PrivateKey
---- ---- ------- ---- ----------
tidb-test tidb v8.1.0 /root/.tiup/storage/cluster/clusters/tidb-test /root/.tiup/storage/cluster/clusters/tidb-test/ssh/id_rsa
二、操作步骤
1、确保两台tidb均安装有TiCDC,已安装的可以忽略这一步,以172.16.22.41为例
在已经安装好的集群新增TiCDC
# vim scale-out.yaml
cdc_servers:
- host: 172.16.22.41
# tiup cluster scale-out tidb-test scale-out.yaml
# tiup cluster display tidb-test
Checking updates for component cluster... Timedout (after 2s)
Cluster type: tidb
Cluster name: tidb-test
Cluster version: v8.1.0
Deploy user: tidb
SSH type: builtin
Dashboard URL: http://172.16.22.41:2379/dashboard
Grafana URL: http://172.16.22.41:3000
ID Role Host Ports OS/Arch Status Data Dir Deploy Dir
-- ---- ---- ----- ------- ------ -------- ----------
172.16.22.41:8300 cdc 172.16.22.41 8300 linux/x86_64 Up /tidb
... ...
2、使用dumpling工具全量备份,在tidb1上操作
tiup安装dumpling
# tiup install dumpling
# cd /root/.tiup/components/dumpling/v8.2.0/
# ./dumpling -u root -p qP5028ks-7^c*9@ejd -P 4000 -h 127.0.0.1 --filetype sql -t 8 -o /tmp/test -r 200000 -F 256MiB
参数说明参考官方文档:https://docs.pingcap.com/zh/tidb/stable/dumpling-overview
3、使用Lightning工具恢复全量备份数据到tidb2,在tidb1上操作
安装 TiDB Lightning 的最新版本
# tiup install tidb-lightning
创建配置文件 tidb-lightning.toml
# vim tidb-lightning.toml
[lightning]
# 日志
level = "info"
file = "tidb-lightning.log"
[tikv-importer]
# 选择使用的导入模式
backend = "local"
# 设置排序的键值对的临时存放地址,目标路径需要是一个空目录
sorted-kv-dir = "/mnt/"
[mydumper]
# 源数据目录。要跟上面导出文件的目录一致
data-source-dir = "/tmp/test/"
# 配置通配符规则,默认规则会过滤 mysql、sys、INFORMATION_SCHEMA、PERFORMANCE_SCHEMA、METRICS_SCHEMA、INSPECTION_SCHEMA 系统数据库下的所有表
# 若不配置该项,导入系统表时会出现“找不到 schema”的异常
filter = ['*.*', '!mysql.*', '!sys.*', '!INFORMATION_SCHEMA.*', '!PERFORMANCE_SCHEMA.*', '!METRICS_SCHEMA.*', '!INSPECTION_SCHEMA.*']
[tidb]
# 目标集群的信息
host = "172.16.22.42"
port = 4000
user = "root"
password = "g_-*12f7E9n56kr@Ge"
# 表架构信息在从 TiDB 的“状态端口”获取。
status-port = 10080
# 集群 pd 的地址。从 v7.6.0 开始支持设置多个地址。
pd-addr = "172.16.22.42:2379"
运行 tidb-lightning。为避免直接在命令行使用 nohup 启动程序时因 SIGHUP 信号导致的程序退出,建议将 nohup 命令放入脚本中
# vim run.sh
#!/bin/bash
nohup tiup tidb-lightning -config tidb-lightning.toml > nohup.out &
# chmod 755 run.sh
# ./run.sh
# tail tidb-lightning.log
[2024/08/01 17:12:39.516 +08:00] [INFO] [import.go:1407] ["cleanup task metas"]
[2024/08/01 17:12:39.517 +08:00] [INFO] [client.go:347] ["[pd] http client closed"] [source=tikv-driver]
[2024/08/01 17:12:39.519 +08:00] [INFO] [import.go:995] ["everything imported, stopping periodic actions"]
[2024/08/01 17:12:39.530 +08:00] [INFO] [import.go:1646] ["skip full compaction"]
[2024/08/01 17:12:39.530 +08:00] [INFO] [import.go:1796] ["clean checkpoints start"] [keepAfterSuccess=remove] [taskID=1722503550707086763]
[2024/08/01 17:12:39.530 +08:00] [INFO] [import.go:1804] ["clean checkpoints completed"] [keepAfterSuccess=remove] [taskID=1722503550707086763] [takeTime=115.102µs] []
[2024/08/01 17:12:39.531 +08:00] [INFO] [import.go:578] ["the whole procedure completed"] [takeTime=8.739582335s] []
[2024/08/01 17:12:39.533 +08:00] [INFO] [client.go:347] ["[pd] http client closed"] [source=lightning]
[2024/08/01 17:12:39.534 +08:00] [INFO] [checksum.go:510] ["service safe point keeper exited"]
[2024/08/01 17:12:39.534 +08:00] [INFO] [main.go:110] ["tidb lightning exit"] [finished=true]
若导入成功,日志的最后一行会显示 tidb lightning exit
4、使用TiCDC实现增量数据同步,在tidb1上操作
创建同步任务,复制增量数据
# cd /tidb-deploy/cdc-8300/bin/
# ./cdc cli changefeed create --server=http://172.16.22.41:8300 --sink-uri="mysql://root:g_-*12f7E9n56kr@Ge@172.16.22.42:4000/" --changefeed-id="simple-replication-task"
执行后出现以下内容即为创建同步任务成功:
Create changefeed successfully!
ID: simple-replication-task
注意:如果出现报错[WARN] Some tables are not eligible to replicate, because they do not have a primary key or a not-null unique key,报错指出特定的数据表不符合复制的要求,因为它们没有主键(Primary Key)或者非空的唯一键(Unique Key),可以尝试通过添加主键或唯一键解决
参数说明参考官方文档:https://docs.pingcap.com/zh/tidb/stable/ticdc-sink-to-mysql#sink-uri-%E9%85%8D%E7%BD%AE-mysqltidb
5、增量数据验证
DBeaver分别连接两个TIDB,全量同步后两个t1表状态,数据一致
往tidb1的t1表插入数据
刷新tidb2的t1表,发现数据同步成功
注意:这个是在tidb1上创建的同步任务,只有在tidb1上插入数据的时候,tidb2可以同步上,反过来在tidb2上插入数据,tidb1上是无法同步的