title | summary |
---|---|
使用 OpenAPI 运维集群 |
了解如何使用 OpenAPI 接口来管理集群状态和数据同步。 |
警告:
当前该功能为实验特性,默认关闭,不建议在生产环境中使用。
DM 提供 OpenAPI 功能,你可以通过 OpenAPI 对 DM 集群进行查询和运维操作。OpenAPI 的总体功能和 dmctl 工具类似。如需开启该功能,请在 DM-master 的配置文件中增加如下配置项:
[experimental]
openapi = true
注意:
DM 提供符合 OpenAPI 3.0.0 标准的 Spec 文档,其中包含了所有 API 的请求参数和返回体,你可自行复制到如 Swagger Editor 等工具中在线预览文档。
部署 DM-master 后,你可访问
http://{master-addr}/api/v1/docs
在线预览文档。
你可以通过 OpenAPI 完成 DM 集群的如下运维操作:
- 创建数据源
- 获取数据源列表
- 删除数据源
- 获取数据源状态
- 对数据源开启 relay-log 功能
- 对数据源停止 relay-log 功能
- 对数据源暂停 relay-log 功能
- 对数据源恢复 relay-log 功能
- 更改数据源和 DM-worker 的绑定关系
- 获取数据源的数据库名列表
- 获取数据源的指定数据库的表名列表
- 创建同步任务
- 获取同步任务列表
- 停止同步任务
- 获取同步任务状态
- 暂停同步任务
- 恢复同步任务
- 获取同步任务关联数据源的数据库名列表
- 获取同步任务关联数据源的数据表名列表
- 获取同步任务关联数据源的数据表的创建语句
- 更新同步任务关联数据源的数据表的创建语句
- 删除同步任务关联数据源的数据表
本文档以下部分描述当前提供的 API 的具体使用方法。
对 API 发起的请求后,如发生错误,返回错误信息的格式如下所示:
{
"error_msg": "",
"error_code": ""
}
如上所示,error_msg
描述错误信息,error_code
则是对应的错误码。
该接口是一个同步接口,请求成功会返回对应节点的状态信息。
GET /api/v1/cluster/masters
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/cluster/masters' \
-H 'accept: application/json'
{
"total": 1,
"data": [
{
"name": "master1",
"alive": true,
"leader": true,
"addr": "127.0.0.1:8261"
}
]
}
该接口是一个同步接口,请求成功后返回体的 Status Code 是 204。
DELETE /api/v1/cluster/masters/{master-name}
{{< copyable "shell-regular" >}}
curl -X 'DELETE' \
'http://127.0.0.1:8261/api/v1/cluster/masters/master1' \
-H 'accept: */*'
该接口是一个同步接口,请求成功会返回对应节点的状态信息。
GET /api/v1/cluster/workers
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/cluster/workers' \
-H 'accept: application/json'
{
"total": 1,
"data": [
{
"name": "worker1",
"addr": "127.0.0.1:8261",
"bound_stage": "bound",
"bound_source_name": "mysql-01"
}
]
}
该接口是一个同步接口,请求成功后返回体的 Status Code 是 204。
DELETE /api/v1/cluster/workers/{worker-name}
{{< copyable "shell-regular" >}}
curl -X 'DELETE' \
'http://127.0.0.1:8261/api/v1/cluster/workers/worker1' \
-H 'accept: */*'
该接口是一个同步接口,请求成功会返回对应数据源信息。
POST /api/v1/sources
{{< copyable "shell-regular" >}}
curl -X 'POST' \
'http://127.0.0.1:8261/api/v1/sources' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"source_name": "mysql-01",
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"enable_gtid": false,
"security": {
"ssl_ca_content": "",
"ssl_cert_content": "",
"ssl_key_content": "",
"cert_allowed_cn": [
"string"
]
},
"purge": {
"interval": 3600,
"expires": 0,
"remain_space": 15
}
}'
{
"source_name": "mysql-01",
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"enable_gtid": false,
"security": {
"ssl_ca_content": "",
"ssl_cert_content": "",
"ssl_key_content": "",
"cert_allowed_cn": [
"string"
]
},
"purge": {
"interval": 3600,
"expires": 0,
"remain_space": 15
},
"status_list": [
{
"source_name": "mysql-replica-01",
"worker_name": "worker-1",
"relay_status": {
"master_binlog": "(mysql-bin.000001, 1979)",
"master_binlog_gtid": "e9a1fc22-ec08-11e9-b2ac-0242ac110003:1-7849",
"relay_dir": "./sub_dir",
"relay_binlog_gtid": "e9a1fc22-ec08-11e9-b2ac-0242ac110003:1-7849",
"relay_catch_up_master": true,
"stage": "Running"
},
"error_msg": "string"
}
]
}
该接口是一个同步接口,请求成功会返回数据源列表信息。
GET /api/v1/sources
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/sources?with_status=true' \
-H 'accept: application/json'
{
"data": [
{
"enable_gtid": false,
"host": "127.0.0.1",
"password": "******",
"port": 3306,
"purge": {
"expires": 0,
"interval": 3600,
"remain_space": 15
},
"security": null,
"source_name": "mysql-01",
"user": "root"
},
{
"enable_gtid": false,
"host": "127.0.0.1",
"password": "******",
"port": 3307,
"purge": {
"expires": 0,
"interval": 3600,
"remain_space": 15
},
"security": null,
"source_name": "mysql-02",
"user": "root"
}
],
"total": 2
}
该接口是一个同步接口,请求成功后返回体的 Status Code 是 204。
DELETE /api/v1/sources/{source-name}
{{< copyable "shell-regular" >}}
curl -X 'DELETE' \
'http://127.0.0.1:8261/api/v1/sources/mysql-01?force=true' \
-H 'accept: application/json'
该接口是一个同步接口,请求成功会返回对应节点的状态信息。
GET /api/v1/sources/{source-name}/status
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/sources/mysql-replica-01/status' \
-H 'accept: application/json'
{
"total": 1,
"data": [
{
"source_name": "mysql-replica-01",
"worker_name": "worker-1",
"relay_status": {
"master_binlog": "(mysql-bin.000001, 1979)",
"master_binlog_gtid": "e9a1fc22-ec08-11e9-b2ac-0242ac110003:1-7849",
"relay_dir": "./sub_dir",
"relay_binlog_gtid": "e9a1fc22-ec08-11e9-b2ac-0242ac110003:1-7849",
"relay_catch_up_master": true,
"stage": "Running"
},
"error_msg": "string"
}
]
}
这是一个异步接口,请求成功的 Status Code 是 200,可通过获取数据源状态接口获取最新的状态。
PATCH /api/v1/sources/{source-name}/start-relay
{{< copyable "shell-regular" >}}
curl -X 'PATCH' \
'http://127.0.0.1:8261/api/v1/sources/mysql-01/start-relay' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"worker_name_list": [
"worker-1"
],
"relay_binlog_name": "mysql-bin.000002",
"relay_binlog_gtid": "e9a1fc22-ec08-11e9-b2ac-0242ac110003:1-7849",
"relay_dir": "./relay_log"
}'
这是一个异步接口,请求成功的 Status Code 是 200,可通过获取数据源状态接口获取最新的状态。
PATCH /api/v1/sources/{source-name}/stop-relay
{{< copyable "shell-regular" >}}
curl -X 'PATCH' \
'http://127.0.0.1:8261/api/v1/sources/mysql-01/stop-relay' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"worker_name_list": [
"worker-1"
]
}'
这是一个异步接口,请求成功的 Status Code 是 200,可通过获取数据源状态接口获取最新的状态。
PATCH /api/v1/sources/{source-name}/pause-relay
{{< copyable "shell-regular" >}}
curl -X 'PATCH' \
'http://127.0.0.1:8261/api/v1/sources/mysql-01/pause-relay' \
-H 'accept: */*'
这是一个异步接口,请求成功的 Status Code 是 200,可通过获取数据源状态接口获取最新的状态。
PATCH /api/v1/sources/{source-name}/resume-relay
{{< copyable "shell-regular" >}}
curl -X 'PATCH' \
'http://127.0.0.1:8261/api/v1/sources/mysql-01/resume-relay' \
-H 'accept: */*'
这是一个异步接口,请求成功的 Status Code 是 200,可通过获取 DM-worker 节点信息接口获取最新的状态。
PATCH /api/v1/sources/{source-name}/transfer
{{< copyable "shell-regular" >}}
curl -X 'PATCH' \
'http://127.0.0.1:8261/api/v1/sources/mysql-01/transfer' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"worker_name": "worker-1"
}'
该接口是一个同步接口,请求成功会返回对应的列表。
GET /api/v1/sources/{source-name}/schemas
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/sources/source-1/schemas' \
-H 'accept: application/json'
[
"db1"
]
该接口是一个同步接口,请求成功会返回对应的列表。
GET /api/v1/sources/{source-name}/schemas/{schema-name}
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/sources/source-1/schemas/db1' \
-H 'accept: application/json'
[
"table1"
]
这是一个异步接口,请求成功的 Status Code 是 200,可通过获取同步任务状态接口获取最新的状态。
POST /api/v1/tasks
{{< copyable "shell-regular" >}}
curl -X 'POST' \
'http://127.0.0.1:8261/api/v1/tasks' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"remove_meta": false,
"task": {
"name": "task-1",
"task_mode": "all",
"shard_mode": "pessimistic",
"meta_schema": "dm-meta",
"enhance_online_schema_change": true,
"on_duplicate": "overwrite",
"target_config": {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"security": {
"ssl_ca_content": "",
"ssl_cert_content": "",
"ssl_key_content": "",
"cert_allowed_cn": [
"string"
]
}
},
"binlog_filter_rule": {
"rule-1": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
},
"rule-2": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
},
"rule-3": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
}
},
"table_migrate_rule": [
{
"source": {
"source_name": "source-name",
"schema": "db-*",
"table": "tb-*"
},
"target": {
"schema": "db1",
"table": "tb1"
},
"binlog_filter_rule": [
"rule-1",
"rule-2",
"rule-3",
]
}
],
"source_config": {
"full_migrate_conf": {
"export_threads": 4,
"import_threads": 16,
"data_dir": "./exported_data",
"consistency": "auto"
},
"incr_migrate_conf": {
"repl_threads": 16,
"repl_batch": 100
},
"source_conf": [
{
"source_name": "mysql-replica-01",
"binlog_name": "binlog.000001",
"binlog_pos": 4,
"binlog_gtid": "03fc0263-28c7-11e7-a653-6c0b84d59f30:1-7041423,05474d3c-28c7-11e7-8352-203db246dd3d:1-170"
}
]
}
},
"source_name_list": [
"source-1"
]
}'
{
"name": "task-1",
"task_mode": "all",
"shard_mode": "pessimistic",
"meta_schema": "dm-meta",
"enhance_online_schema_change": true,
"on_duplicate": "overwrite",
"target_config": {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"security": {
"ssl_ca_content": "",
"ssl_cert_content": "",
"ssl_key_content": "",
"cert_allowed_cn": [
"string"
]
}
},
"binlog_filter_rule": {
"rule-1": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
},
"rule-2": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
},
"rule-3": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
}
},
"table_migrate_rule": [
{
"source": {
"source_name": "source-name",
"schema": "db-*",
"table": "tb-*"
},
"target": {
"schema": "db1",
"table": "tb1"
},
"binlog_filter_rule": [
"rule-1",
"rule-2",
"rule-3",
]
}
],
"source_config": {
"full_migrate_conf": {
"export_threads": 4,
"import_threads": 16,
"data_dir": "./exported_data",
"consistency": "auto"
},
"incr_migrate_conf": {
"repl_threads": 16,
"repl_batch": 100
},
"source_conf": [
{
"source_name": "mysql-replica-01",
"binlog_name": "binlog.000001",
"binlog_pos": 4,
"binlog_gtid": "03fc0263-28c7-11e7-a653-6c0b84d59f30:1-7041423,05474d3c-28c7-11e7-8352-203db246dd3d:1-170"
}
]
}
}
该接口是一个同步接口,请求成功会返回对应的同步任务信息。
GET /api/v1/tasks
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/tasks' \
-H 'accept: application/json'
{
"total": 2,
"data": [
{
"name": "task-1",
"task_mode": "all",
"shard_mode": "pessimistic",
"meta_schema": "dm-meta",
"enhance_online_schema_change": true,
"on_duplicate": "overwrite",
"target_config": {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"security": {
"ssl_ca_content": "",
"ssl_cert_content": "",
"ssl_key_content": "",
"cert_allowed_cn": [
"string"
]
}
},
"binlog_filter_rule": {
"rule-1": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
},
"rule-2": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
},
"rule-3": {
"ignore_event": [
"all dml"
],
"ignore_sql": [
"^Drop"
]
}
},
"table_migrate_rule": [
{
"source": {
"source_name": "source-name",
"schema": "db-*",
"table": "tb-*"
},
"target": {
"schema": "db1",
"table": "tb1"
},
"binlog_filter_rule": [
"rule-1",
"rule-2",
"rule-3",
]
}
],
"source_config": {
"full_migrate_conf": {
"export_threads": 4,
"import_threads": 16,
"data_dir": "./exported_data",
"consistency": "auto"
},
"incr_migrate_conf": {
"repl_threads": 16,
"repl_batch": 100
},
"source_conf": [
{
"source_name": "mysql-replica-01",
"binlog_name": "binlog.000001",
"binlog_pos": 4,
"binlog_gtid": "03fc0263-28c7-11e7-a653-6c0b84d59f30:1-7041423,05474d3c-28c7-11e7-8352-203db246dd3d:1-170"
}
]
}
}
]
}
这是一个异步接口,请求成功的 Status Code 是 204,可通过获取同步任务状态接口获取最新的状态。
DELETE /api/v1/tasks/{task-name}
{{< copyable "shell-regular" >}}
curl -X 'DELETE' \
'http://127.0.0.1:8261/api/v1/tasks/task-1' \
-H 'accept: */*'
该接口是一个同步接口,请求成功会返回对应节点的状态信息。
GET /api/v1/tasks/task-1/status
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/tasks/task-1/status?stage=running' \
-H 'accept: application/json'
{
"total": 1,
"data": [
{
"name": "string",
"source_name": "string",
"worker_name": "string",
"stage": "runing",
"unit": "sync",
"unresolved_ddl_lock_id": "string",
"load_status": {
"finished_bytes": 0,
"total_bytes": 0,
"progress": "string",
"meta_binlog": "string",
"meta_binlog_gtid": "string"
},
"sync_status": {
"total_events": 0,
"total_tps": 0,
"recent_tps": 0,
"master_binlog": "string",
"master_binlog_gtid": "string",
"syncer_binlog": "string",
"syncer_binlog_gtid": "string",
"blocking_ddls": [
"string"
],
"unresolved_groups": [
{
"target": "string",
"ddl_list": [
"string"
],
"first_location": "string",
"synced": [
"string"
],
"unsynced": [
"string"
]
}
],
"synced": true,
"binlog_type": "string",
"seconds_behind_master": 0
}
}
]
}
这是一个异步接口,请求成功的 Status Code 是 200,可通过获取同步任务状态接口获取最新的状态。
PATCH /api/v1/tasks/task-1/pause
{{< copyable "shell-regular" >}}
curl -X 'PATCH' \
'http://127.0.0.1:8261/api/v1/tasks/task-1/pause' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '[
"source-1"
]'
这是一个异步接口,请求成功的 Status Code 是 200,可通过获取同步任务状态接口获取最新的状态。
PATCH /api/v1/tasks/task-1/resume
{{< copyable "shell-regular" >}}
curl -X 'PATCH' \
'http://127.0.0.1:8261/api/v1/tasks/task-1/resume' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '[
"source-1"
]'
该接口是一个同步接口,请求成功会返回对应的列表。
GET /api/v1/tasks/{task-name}/sources/{source-name}/schemas
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/tasks/task-1/sources/source-1/schemas' \
-H 'accept: application/json'
[
"db1"
]
该接口是一个同步接口,请求成功会返回对应的列表。
GET /api/v1/tasks/{task-name}/sources/{source-name}/schemas/{schema-name}
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/tasks/task-1/sources/source-1/schemas/db1' \
-H 'accept: application/json'
[
"table1"
]
该接口是一个同步接口,请求成功会返回对应的创建语句。
GET /api/v1/tasks/{task-name}/sources/{source-name}/schemas/{schema-name}/{table-name}
{{< copyable "shell-regular" >}}
curl -X 'GET' \
'http://127.0.0.1:8261/api/v1/tasks/task-1/sources/source-1/schemas/db1/table1' \
-H 'accept: application/json'
{
"schema_name": "db1",
"table_name": "table1",
"schema_create_sql": "CREATE TABLE `t1` (`id` int(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
}
该接口是一个同步接口,返回体的 Status Code 是 200。
PATCH /api/v1/tasks/{task-name}/sources/{source-name}/schemas/{schema-name}/{table-name}
{{< copyable "shell-regular" >}}
curl -X 'PUT' \
'http://127.0.0.1:8261/api/v1/tasks/task-1/sources/task-1/schemas/db1/table1' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"sql_content": "CREATE TABLE `t1` ( `c1` int(11) DEFAULT NULL, `c2` int(11) DEFAULT NULL, `c3` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;",
"flush": true,
"sync": true
}'
该接口是一个同步接口,返回体的 Status Code 是 200。
DELETE /api/v1/tasks/{task-name}/sources/{source-name}/schemas/{schema-name}/{table-name}
{{< copyable "shell-regular" >}}
curl -X 'DELETE' \
'http://127.0.0.1:8261/api/v1/tasks/task-1/sources/source-1/schemas/db1/table1' \
-H 'accept: */*'