• 数据迁移概述
    • 概述
    • 两种迁移场景
    • MySQL 开启 binlog
    • 使用 checker 进行 Schema 检查
      • 下载 TiDB 工具集 (Linux)
      • 使用 checker 检查的一个示范
      • 一个无法迁移的 table 例子

    数据迁移概述

    概述

    该文档详细介绍了如何将 MySQL 的数据迁移到 TiDB。

    这里我们假定 MySQL 以及 TiDB 服务信息如下:

    Name Address Port User Password
    MySQL 127.0.0.1 3306 root *
    TiDB 127.0.0.1 4000 root *

    在这个数据迁移过程中,我们会用到下面四个工具:

    • checker 检查 schema 能否被 TiDB 兼容
    • mydumper 从 MySQL 导出数据
    • loader 导入数据到 TiDB
    • syncer 增量同步 MySQL 数据到 TiDB

    两种迁移场景

    • 第一种场景:只全量导入历史数据 (需要 checker + mydumper + loader);
    • 第二种场景:全量导入历史数据后,通过增量的方式同步新的数据 (需要 checker + mydumper + loader + syncer)。该场景需要提前开启 binlog 且格式必须为 ROW。

    MySQL 开启 binlog

    注意: 只有上文提到的第二种场景才需要在 dump 数据之前先开启 binlog

    • MySQL 开启 binlog 功能,参考 Setting the Replication Master Configuration
    • Binlog 格式必须使用 ROW format,这也是 MySQL 5.7 之后推荐的 binlog 格式,可以使用如下语句打开:

      1. SET GLOBAL binlog_format = ROW;

    使用 checker 进行 Schema 检查

    在迁移之前,我们可以使用 TiDB 的 checker 工具,来预先检查 TiDB 是否能支持需要迁移的 table schema。如果 check 某个 table schema 失败,表明 TiDB 当前并不支持,我们不能对该 table 里面的数据进行迁移。checker 包含在 TiDB 工具集里面,我们可以直接下载。

    下载 TiDB 工具集 (Linux)

    1. # 下载 tool 压缩包
    2. wget http://download.pingcap.org/tidb-enterprise-tools-latest-linux-amd64.tar.gz
    3. wget http://download.pingcap.org/tidb-enterprise-tools-latest-linux-amd64.sha256
    4. # 检查文件完整性,返回 ok 则正确
    5. sha256sum -c tidb-enterprise-tools-latest-linux-amd64.sha256
    6. # 解开压缩包
    7. tar -xzf tidb-enterprise-tools-latest-linux-amd64.tar.gz
    8. cd tidb-enterprise-tools-latest-linux-amd64

    使用 checker 检查的一个示范

    • 在 MySQL 的 test database 里面创建几张表,并插入数据:

      1. USE test;
      2. CREATE TABLE t1 (id INT, age INT, PRIMARY KEY(id)) ENGINE=InnoDB;
      3. CREATE TABLE t2 (id INT, name VARCHAR(256), PRIMARY KEY(id)) ENGINE=InnoDB;
      4. INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3);
      5. INSERT INTO t2 VALUES (1, "a"), (2, "b"), (3, "c");
    • 使用 checker 检查 test database 里面所有的 table

      1. ./bin/checker -host 127.0.0.1 -port 3306 -user root test
      2. 2016/10/27 13:11:49 checker.go:48: [info] Checking database test
      3. 2016/10/27 13:11:49 main.go:37: [info] Database DSN: root:@tcp(127.0.0.1:3306)/test?charset=utf8
      4. 2016/10/27 13:11:49 checker.go:63: [info] Checking table t1
      5. 2016/10/27 13:11:49 checker.go:69: [info] Check table t1 succ
      6. 2016/10/27 13:11:49 checker.go:63: [info] Checking table t2
      7. 2016/10/27 13:11:49 checker.go:69: [info] Check table t2 succ
    • 使用 checker 检查 test database 里面某一个 table

      这里,假设我们只需要迁移 table t1

      1. ./bin/checker -host 127.0.0.1 -port 3306 -user root test t1
      2. 2016/10/27 13:13:56 checker.go:48: [info] Checking database test
      3. 2016/10/27 13:13:56 main.go:37: [info] Database DSN: root:@tcp(127.0.0.1:3306)/test?charset=utf8
      4. 2016/10/27 13:13:56 checker.go:63: [info] Checking table t1
      5. 2016/10/27 13:13:56 checker.go:69: [info] Check table t1 succ
      6. Check database succ!

    一个无法迁移的 table 例子

    我们在 MySQL 里面创建如下表:

    1. CREATE TABLE t_error ( a INT NOT NULL, PRIMARY KEY (a))
    2. ENGINE=InnoDB TABLESPACE ts1
    3. PARTITION BY RANGE (a) PARTITIONS 3 (
    4. PARTITION P1 VALUES LESS THAN (2),
    5. PARTITION P2 VALUES LESS THAN (4) TABLESPACE ts2,
    6. PARTITION P3 VALUES LESS THAN (6) TABLESPACE ts3);

    使用 checker 进行检查,会报错,表明我们没法迁移 t_error 这张表。

    1. ./bin/checker -host 127.0.0.1 -port 3306 -user root test t_error
    2. 2017/08/04 11:14:35 checker.go:48: [info] Checking database test
    3. 2017/08/04 11:14:35 main.go:39: [info] Database DSN: root:@tcp(127.0.0.1:3306)/test?charset=utf8
    4. 2017/08/04 11:14:35 checker.go:63: [info] Checking table t1
    5. 2017/08/04 11:14:35 checker.go:67: [error] Check table t1 failed with err: line 3 column 29 near " ENGINE=InnoDB DEFAULT CHARSET=latin1
    6. /*!50100 PARTITION BY RANGE (a)
    7. (PARTITION P1 VALUES LESS THAN (2) ENGINE = InnoDB,
    8. PARTITION P2 VALUES LESS THAN (4) TABLESPACE = ts2 ENGINE = InnoDB,
    9. PARTITION P3 VALUES LESS THAN (6) TABLESPACE = ts3 ENGINE = InnoDB) */" (total length 354)
    10. github.com/pingcap/tidb/parser/yy_parser.go:96:
    11. github.com/pingcap/tidb/parser/yy_parser.go:109:
    12. /home/jenkins/workspace/build_tidb_tools_master/go/src/github.com/pingcap/tidb-tools/checker/checker.go:122: parse CREATE TABLE `t1` (
    13. `a` int(11) NOT NULL,
    14. PRIMARY KEY (`a`)
    15. ) /*!50100 TABLESPACE ts1 */ ENGINE=InnoDB DEFAULT CHARSET=latin1
    16. /*!50100 PARTITION BY RANGE (a)
    17. (PARTITION P1 VALUES LESS THAN (2) ENGINE = InnoDB,
    18. PARTITION P2 VALUES LESS THAN (4) TABLESPACE = ts2 ENGINE = InnoDB,
    19. PARTITION P3 VALUES LESS THAN (6) TABLESPACE = ts3 ENGINE = InnoDB) */ error
    20. /home/jenkins/workspace/build_tidb_tools_master/go/src/github.com/pingcap/tidb-tools/checker/checker.go:114:
    21. 2017/08/04 11:14:35 main.go:83: [error] Check database test with 1 errors and 0 warnings.