tidb迁移tidb

tidb迁移tidb

创建连接工具

使用官方推荐BR(backup&restoer)备份数据 ,使用navicat、datagrip会出现backup连接响应时长问题,推荐使用mysql-cli工具

快速使用deployment创建mysql cli

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysqlcli
  namespace: tidb-cluster
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysqlcli
  template:
    metadata:
      labels:
        app: mysqlcli
    spec:
      containers:
        - name: mysqlcli
          image: mysql:8.0
          ports:
            - containerPort: 3306
              protocol: TCP
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root

等待启动

启动完成,exec连接上终端

使用 mysql -uroot -pxxxx -hmysql 连接

关闭 GC

为了保证增量迁移过程中新写入的数据不丢失,在开始备份之前,需要关闭上游集群的垃圾回收 (GC) 机制,以确保系统不再清理历史数据。

执行如下命令关闭 GC:

    MySQL [none]> SET GLOBAL tidb_gc_enable=FALSE;
    Query OK, 0 rows affected (0.01 sec)

查询 tidb_gc_enable 的取值,判断 GC 是否已关闭:

    MySQL [none]> SELECT @@global.tidb_gc_enable;
    +-------------------------+:
    | @@global.tidb_gc_enable |
    +-------------------------+
    |                       0 |
    +-------------------------+
    1 row in set (0.00 sec)

备份数据

在上游集群中执行 BACKUP 语句备份数据

mysql> BACKUP DATABASE * TO 's3://shenyi-bigdata-test/tidb-test-upstream-backup/test-upstream?access-key=########&secret-access-key=##########&region=us-east-2&force-path-style=true' RATE_LIMIT = 300 MB/SECOND;

//注意: * 可以为单个database导出,例如 app  (BACKUP DATABASE app TO)
+------------------------------------------------------------------+-------------+--------------------+---------------------+---------------------+
 Destination                                                      | Size        | BackupTS           | Queue Time          | Execution Time      |
+------------------------------------------------------------------+-------------+--------------------+---------------------+---------------------+
 s3://shenyi-bigdata-test/tidb-test-upstream-backup/test-upstream | 11669347639 | 445128154804387841 | 2023-10-23 10:45:54 | 2023-10-23 10:45:54 |
+------------------------------------------------------------------+-------------+--------------------+---------------------+---------------------+
1 row in set (16 min 28.30 sec)

备份语句提交成功后,TiDB 会返回关于备份数据的元信息,这里需要重点关注 BackupTS,它意味着该时间点之前数据会被备份,后边的教程中,本文将使用 BackupTS 作为数据校验截止时间和 TiCDC 增量扫描的开始时间

恢复数据

在下游集群中执行 RESTORE 语句恢复数据:

mysql> RESTORE DATABASE * FROM 's3://shenyi-bigdata-test/tidb-test-upstream-backup/test-upstream?access-key=#####&secret-access-key=#####&region=us-east-2';

//注意: * 可以为单个database 例如 app(RESTORE DATABASE app FROM) 要是backup为单database,restore也需要为单库

+-----------------------------------------------------------------------+------------+--------------------+--------------------+---------------------+---------------------+
 Destination                                                           | Size       | BackupTS           | Cluster TS         | Queue Time          | Execution Time      |
+-----------------------------------------------------------------------+------------+--------------------+--------------------+---------------------+---------------------+
 s3://shenyi-bigdata-test/tidb-test-upstream-backup/test-upstream | 11669347639 | 445128154804387841 | 445136929260306437 | 2023-10-23 20:03:41 | 2023-10-23 20:03:41 |
+-----------------------------------------------------------------------+------------+--------------------+--------------------+---------------------+---------------------+
1 row in set (7 min 8.76 sec)
Rows matched: 1  Changed: 1  Warnings: 0

导入异常

在恢复数据中,存在某些其他的未知问题,导致无法导入会提示如下错误

ERROR 8125 (HY000): Restore failed: [ddl:8204]DDL job rollback, error msg: Invalid DDL job%!(EXTRA string=table, model.SchemaState=public)

遇数据量不大,变动情况小。可以使用navicat或其他工具,直接同步数据

cdc同步增量数据

进入cdc容器内

cdc cli changefeed create --dp=http://basic-dp:2379 --sink-uri="mysql://root:xxxx@a6b8c8327097348b3be36ce6e070e695-87dcc86e4b8af9c7.elb.us-east-2.amazonaws.com:3306" --changefeed-id="upstream-to-downstream" --start-ts="445128154804387841" --config Changefeed.toml


cat Changefeed.toml
enable-old-value = true
force-replicate = true
Licensed under CC BY-NC-SA 4.0