一键导入

关键概念介绍

image.png

一键导入

MySQL Master/Slave/Replica 相关命令只支持增量日志的同步和消费, 如果需要构建结构和数据相同的Slave实例,需要对已有表进行结构迁移存量数据的全量迁移。为了方便用户创建数据一致的镜像实例,PolarDB-X Replica 在MySQL CHANGE MASTER TO 命令的基础上新增 MODE 参数,当 MODE 参数为 IMAGE 时,表示使用镜像模式建立主备链路,即实现存量结构和数据的“一键导入”。

结构迁移

从源实例获取该链路包含的库表结构,在目标实例上创建。如已存在库/表,则跳过存在的库/表。

全量迁移

源实例上存量数据的迁移。

增量同步

基于binlog的增量同步。

使用限制

  • 目前源端仅支持标准 MySQL 实例。
  • 目前结构迁移只支持创建默认模式数据库即 DRDS 模式数据库,只支持创建和源端表结构完全一致的目标表。如需定制库的模式和表结构可自行建库表后使用一键导入功能。
  • 一键导入默认过滤如下 MySQL系统表:SYS, MYSQL, INFORMATION_SCHEMA。为防止对相关系统表产生影响,建议设置REPLICATE_DO_DB为需要导入的库后再启动链路。
  • 一键导入暂不支持无主键表,如 Replica包含的表 中有无主键表,会直接忽略无主键表。
  • 全量阶段在 Replica包含的表 执行DDL可能会导致链路中断。

SQL命令

您可以执行如下命令使用一键导入功能

CHANGE MASTER TO

在 MySQL CHANGE MASTER TO 命令上新增 MODE 参数,若该参数值为 IMAGE,则表示建立一键导入链路,其余情况表示建立普通Replica链路。

SHOW SLAVE STATUS

在 MySQL SHOW SLAVE STATUS 命令上新增 Replicate_Mode 和 Running_Stage 返回参数。其中,Replicate_Mode 用于区分链路为一键导入模式还是普通 Replica 模式;Running_Stage 用于显示此时链路的运行步骤。

其余命令

与MySQL Replica指令保持一致,详见MySQL对应语法

使用示例

CASE 1:

  1. 在源MySQL上创建库表并插入数据: ```sql CREATE DATABASE rpl_test;

USE rpl_test;

CREATE TABLE accounts_test ( balance int(11) NOT NULL, name varchar(32) NOT NULL, gmt_created datetime NOT NULL, id int(11) NOT NULL, remark varchar(32) NOT NULL, PRIMARY KEY (id), KEY auto_shard_key_name USING BTREE (name) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

INSERT INTO accounts_test values (1,'1','2022-12-05 11:00:01',1,'12'); INSERT INTO accounts_test values (12,'12','2022-12-05 11:00:01',12,'12'); INSERT INTO accounts_test values (123,'123','2022-12-05 11:00:01',123,'12'); INSERT INTO accounts_test values (1234,'1234','2022-12-05 11:00:01',1234,'12'); INSERT INTO accounts_test values (12345,'12345','2022-12-05 11:00:01',12345,'12'); INSERT INTO accounts_test values (123456,'123456','2022-12-05 11:00:01',123456,'12'); INSERT INTO accounts_test values (1234567,'1234567','2022-12-05 11:00:01',1234567,'12');


2. 在源MySQL上获取执行完上述语句后的最新binlog位点:
```sql
SHOW MASTER STATUS;

image.png

  1. 在目标 PolarDB-X 上创建 Replica 一键导入链路:

    CHANGE MASTER TO
     MASTER_HOST='import-opensource-test-7qkj-dn-0',
     MASTER_USER='admin',
     MASTER_PASSWORD='l26rfwsr',
     MASTER_PORT=3306,
     MASTER_LOG_FILE='mysql_bin.000001',
     MASTER_LOG_POS='58050130',
     SOURCE_HOST_TYPE=mysql,Mode='IMAGE';
    
  2. 设置需要同步的库表:

    CHANGE REPLICATION FILTER REPLICATE_DO_DB=(rpl_test);
    
  3. 开启一键导入链路:

    START SLAVE;
    
  4. 在目标端查看同步情况: ```sql SHOW DATABASES;

SELECT * FROM rpl_test.accounts_test;

![image.png](../images/import_result2.png)

7. 查看此时链路情况:
```sql
SHOW SLAVE STATUS\G

benchmark_tpcc_load

注:此处 IMAGE 表示一键导入模式,REPLICA_INC表示已完成结构和全量迁移,进入增量迁移步骤。 此时在源端插入数据,并在目标端查看同步情况:效果等同于普通 Replica 链路,不再赘述。

CASE 2:

  1. 参考TPCC测试在源端导入10仓TPCC数据,并开启TPCC测试
    1. 注:因一键导入使用限制,需在bmsql_history表增加自增主键
  2. 同上述流程,建立一键导入链路 ```sql // 源端 SHOW MASTER STATUS;

// 目标端 CHANGE MASTER TO MASTER_HOST='jiyue-test111-tf24-dn-0', MASTER_USER='admin', MASTER_PASSWORD='sp9tn2lq', MASTER_PORT=3306, MASTER_LOG_FILE='mysql_bin.000004', MASTER_LOG_POS='246743757', SOURCE_HOST_TYPE=mysql,Mode='IMAGE';

// 目标端 CHANGE REPLICATION FILTER REPLICATE_DO_DB=(tpcc);

// 目标端 START SLAVE;


3. 观察结果

待2分钟TPCC测试结束后,观察两端数据:
源端:
```sql
mysql> select count(1) from bmsql_config;
+----------+
| count(1) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)

mysql> select count(1) from bmsql_customer;
+----------+
| count(1) |
+----------+
|   300000 |
+----------+
1 row in set (0.03 sec)

mysql> select count(1) from bmsql_district;
+----------+
| count(1) |
+----------+
|      100 |
+----------+
1 row in set (0.00 sec)

mysql> select count(1) from bmsql_history;
+----------+
| count(1) |
+----------+
|   484855 |
+----------+
1 row in set (0.03 sec)

mysql> select count(1) from bmsql_item;
+----------+
| count(1) |
+----------+
|   100000 |
+----------+
1 row in set (0.01 sec)

mysql> select count(1) from bmsql_new_order;
+----------+
| count(1) |
+----------+
|   118559 |
+----------+
1 row in set (0.01 sec)

mysql> select count(1) from bmsql_oorder;
+----------+
| count(1) |
+----------+
|   491928 |
+----------+
1 row in set (0.02 sec)

mysql> select count(1) from bmsql_order_line;
+----------+
| count(1) |
+----------+
|  4921254 |
+----------+
1 row in set (0.15 sec)

mysql> select count(1) from bmsql_stock;
+----------+
| count(1) |
+----------+
|  1000000 |
+----------+
1 row in set (0.06 sec)

mysql> select count(1) from bmsql_warehouse;
+----------+
| count(1) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)

目标端:

mysql> select count(1) from bmsql_config;
+----------+
| count(1) |
+----------+
|        4 |
+----------+
1 row in set (0.08 sec)

mysql> select count(1) from bmsql_customer;
+----------+
| count(1) |
+----------+
|   300000 |
+----------+
1 row in set (0.74 sec)

mysql> select count(1) from bmsql_district;
+----------+
| count(1) |
+----------+
|      100 |
+----------+
1 row in set (0.08 sec)

mysql> select count(1) from bmsql_history;
+----------+
| count(1) |
+----------+
|   484855 |
+----------+
1 row in set (0.18 sec)

mysql> select count(1) from bmsql_item;
+----------+
| count(1) |
+----------+
|   100000 |
+----------+
1 row in set (0.08 sec)

mysql> select count(1) from bmsql_new_order;
+----------+
| count(1) |
+----------+
|   118559 |
+----------+
1 row in set (0.09 sec)

mysql> select count(1) from bmsql_oorder;
+----------+
| count(1) |
+----------+
|   491928 |
+----------+
1 row in set (1.08 sec)

mysql> select count(1) from bmsql_order_line;
+----------+
| count(1) |
+----------+
|  4921254 |
+----------+
1 row in set (0.94 sec)

mysql> select count(1) from bmsql_stock;
+----------+
| count(1) |
+----------+
|  1000000 |
+----------+
1 row in set (0.35 sec)

mysql> select count(1) from bmsql_warehouse;
+----------+
| count(1) |
+----------+
|       10 |
+----------+
1 row in set (0.07 sec)

两端数据一致,共计完成迁移约750w条数据,共计耗时2分钟内。

results matching ""

    No results matching ""