使用手册

使用手册

环境要求

纯 JAVA 开发,JDK 建议 1.8 以上版本。

支持迁移场景如下:

源端目标端
MySQL(5.1.15 ~ 5.7.x)MySQL
PostgreSQL(9.4 ~ )PostgreSQL
openGauss(2.1.0)openGauss

注意

如果后端连接 MySQL 数据库,请下载 mysql-connector-java-5.1.47.jar,并将其放入 ${shardingsphere-proxy}/lib 目录。

权限要求

MySQL 需要开启 binlog,且迁移时所使用用户需要赋予 Replication 相关权限。

  1. +-----------------------------------------+---------------------------------------+
  2. | Variable_name | Value |
  3. +-----------------------------------------+---------------------------------------+
  4. | log_bin | ON |
  5. | binlog_format | ROW |
  6. | binlog_row_image | FULL |
  7. +-----------------------------------------+---------------------------------------+
  8. +------------------------------------------------------------------------------+
  9. |Grants for ${username}@${host} |
  10. +------------------------------------------------------------------------------+
  11. |GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ${username}@${host} |
  12. |....... |
  13. +------------------------------------------------------------------------------+

PostgreSQL 需要开启 test_decoding

DistSQL 接口

弹性迁移组件提供了 DistSQL 接口

预览当前分片规则

示例:

  1. preview select count(1) from t_order;

返回信息:

  1. mysql> preview select count(1) from t_order;
  2. +------------------+--------------------------------+
  3. | data_source_name | sql |
  4. +------------------+--------------------------------+
  5. | ds_0 | select count(1) from t_order_0 |
  6. | ds_0 | select count(1) from t_order_1 |
  7. | ds_1 | select count(1) from t_order_0 |
  8. | ds_1 | select count(1) from t_order_1 |
  9. +------------------+--------------------------------+
  10. 4 rows in set (0.00 sec)

创建迁移任务

  1. 添加新的数据源

详情请参见RDL#数据源资源

示例:

  1. ADD RESOURCE ds_2 (
  2. URL="jdbc:mysql://127.0.0.1:3306/db2?serverTimezone=UTC&useSSL=false",
  3. USER=root,
  4. PASSWORD=root,
  5. PROPERTIES("maximumPoolSize"=10,"idleTimeout"="30000")
  6. );
  7. -- ds_3, ds_4
  1. 修改分片规则

详情请参见RDL#数据分片

示例:

  1. ALTER SHARDING TABLE RULE t_order (
  2. RESOURCES(ds_2, ds_3, ds_4),
  3. SHARDING_COLUMN=order_id,
  4. TYPE(NAME=hash_mod,PROPERTIES("sharding-count"=10)),
  5. GENERATED_KEY(COLUMN=another_id,TYPE(NAME=snowflake,PROPERTIES("worker-id"=123)))
  6. );

比如说RESOURCESsharding-count修改了会触发迁移。

查询所有迁移任务

详情请参见RAL#弹性伸缩

示例:

  1. show scaling list;

返回信息:

  1. mysql> show scaling list;
  2. +--------------------+-----------------------+----------------------+--------+---------------------+---------------------+
  3. | id | tables | sharding_total_count | active | create_time | stop_time |
  4. +--------------------+-----------------------+----------------------+--------+---------------------+---------------------+
  5. | 659853312085983232 | t_order_item, t_order | 2 | 0 | 2021-10-26 20:21:31 | 2021-10-26 20:24:01 |
  6. | 660152090995195904 | t_order_item, t_order | 2 | 0 | 2021-10-27 16:08:43 | 2021-10-27 16:11:00 |
  7. +--------------------+-----------------------+----------------------+--------+---------------------+---------------------+
  8. 2 rows in set (0.04 sec)

查询迁移任务进度

示例:

  1. show scaling status {jobId};

返回信息:

  1. mysql> show scaling status 660152090995195904;
  2. +------+-------------+----------+-------------------------------+--------------------------+
  3. | item | data_source | status | inventory_finished_percentage | incremental_idle_minutes |
  4. +------+-------------+----------+-------------------------------+--------------------------+
  5. | 0 | ds_1 | FINISHED | 100 | 2834 |
  6. | 1 | ds_0 | FINISHED | 100 | 2834 |
  7. +------+-------------+----------+-------------------------------+--------------------------+
  8. 2 rows in set (0.00 sec)

当前迁移任务已完成,新的分片规则已生效。如果迁移失败,新的分片规则不会生效。

status的取值:

取值描述
PREPARING准备中
RUNNING运行中
EXECUTE_INVENTORY_TASK全量迁移中
EXECUTE_INCREMENTAL_TASK增量迁移中
ALMOST_FINISHED基本完成
FINISHED已完成
PREPARING_FAILURE准备阶段失败
EXECUTE_INVENTORY_TASK_FAILURE全量迁移阶段失败
EXECUTE_INCREMENTAL_TASK_FAILURE增量迁移阶段失败

预览新的分片规则是否生效

示例:

  1. preview select count(1) from t_order;

返回信息:

  1. mysql> preview select count(1) from t_order;
  2. +------------------+--------------------------------+
  3. | data_source_name | sql |
  4. +------------------+--------------------------------+
  5. | ds_2 | select count(1) from t_order_0 |
  6. | ds_2 | select count(1) from t_order_1 |
  7. | ds_3 | select count(1) from t_order_0 |
  8. | ds_3 | select count(1) from t_order_1 |
  9. | ds_4 | select count(1) from t_order_0 |
  10. | ds_4 | select count(1) from t_order_1 |
  11. +------------------+--------------------------------+
  12. 6 rows in set (0.01 sec)

其他DistSQL

详情请参见RAL#弹性伸缩