alibabacloud-dms-skill

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Alibaba Cloud DMS Database Read/Write

阿里云DMS数据库读写

Search for target databases and execute SQL queries and data modifications via Alibaba Cloud DMS OpenAPI.
通过阿里云DMS OpenAPI搜索目标数据库,执行SQL查询和数据修改。

Scenario Description

场景描述

This skill implements the following workflow:
  1. Search Target Database — Search databases by keyword to get Database ID
  2. Execute SQL Query — Execute SQL statements on the target database
本技能实现以下工作流:
  1. 搜索目标数据库 — 按关键词搜索数据库获取数据库ID
  2. 执行SQL查询 — 在目标数据库上执行SQL语句

Architecture

架构

User Request → Search Database → Get Database ID → Execute SQL → Return Results
User Request → Search Database → Get Database ID → Execute SQL → Return Results

Prerequisites

前置条件

Pre-check: Aliyun CLI >= 3.3.1 required Run
aliyun version
to verify >= 3.3.1. If not installed or version too low, see
references/cli-installation-guide.md
for installation instructions. Then [MUST] run
aliyun configure set --auto-plugin-install true
to enable automatic plugin installation.
  • Aliyun CLI >= 3.3.1
  • jq (for JSON parsing):
    brew install jq
  • Credentials configured via
    aliyun configure
Pre-check: Alibaba Cloud Credentials Required
Security Rules:
  • NEVER read, echo, or print AK/SK values (e.g.,
    echo $ALIBABA_CLOUD_ACCESS_KEY_ID
    is FORBIDDEN)
  • NEVER ask the user to input AK/SK directly in the conversation or command line
  • NEVER use
    aliyun configure set
    with literal credential values
  • ONLY use
    aliyun configure list
    to check credential status
bash
aliyun configure list
Check the output for a valid profile (AK, STS, or OAuth identity).
If no valid profile exists, STOP here.
  1. Obtain credentials from Alibaba Cloud Console
  2. Configure credentials outside of this session (via
    aliyun configure
    in terminal or environment variables in shell profile)
  3. Return and re-run after
    aliyun configure list
    shows a valid profile
预检查:要求Aliyun CLI >= 3.3.1 运行
aliyun version
验证版本 >= 3.3.1。如果未安装或版本过低, 参考
references/cli-installation-guide.md
查看安装指引。 之后【必须】运行
aliyun configure set --auto-plugin-install true
开启自动插件安装功能。
  • Aliyun CLI >= 3.3.1
  • jq(用于JSON解析):
    brew install jq
  • 已通过
    aliyun configure
    配置凭证
预检查:需要阿里云凭证
安全规则:
  • 严禁读取、回显或打印AK/SK值(例如禁止执行
    echo $ALIBABA_CLOUD_ACCESS_KEY_ID
  • 严禁要求用户在对话或命令行中直接输入AK/SK
  • 严禁使用字面量凭证值执行
    aliyun configure set
  • 仅可使用
    aliyun configure list
    检查凭证状态
bash
aliyun configure list
检查输出中是否存在有效配置(AK、STS或OAuth身份)。
如果不存在有效配置,请停止操作。
  1. 阿里云控制台获取凭证
  2. 在本次会话之外配置凭证(通过终端执行
    aliyun configure
    或在shell配置文件中设置环境变量)
  3. aliyun configure list
    显示有效配置后,返回重新执行

RAM Permissions

RAM权限

[MUST] RAM Permission Pre-check: Verify that the current user has the following RAM permissions before execution. See
references/ram-policies.md
for the complete permission list.
[必须] RAM权限预检查: 执行前请确认当前用户拥有所需RAM权限。 完整权限列表参考
references/ram-policies.md

Parameter Confirmation

参数确认

IMPORTANT: Parameter Confirmation — Before executing any command or API call, ALL user-customizable parameters (e.g., database keyword, SQL statement, db-id, etc.) MUST be confirmed with the user. Do NOT assume or use default values without explicit user approval.
ParameterRequired/OptionalDescriptionDefault
keywordRequiredDatabase search keyword (1-128 chars, alphanumeric)-
db-idRequiredDatabase ID (positive integer, obtained from search)-
sqlRequiredSQL statement to execute (1-10000 chars)-
logicOptionalWhether to use logic database modefalse
forceOptionalConfirm write operations (INSERT/UPDATE/DELETE)false
dry-runOptionalPreview write operations without executingfalse
重要提示:参数确认 — 执行任何命令或API调用前, 所有用户可自定义参数(例如数据库关键词、SQL语句、db-id等) 必须与用户确认。未获得用户明确批准前,不得假设或使用默认值。
参数必填/可选描述默认值
keyword必填数据库搜索关键词(1-128个字符,字母数字)-
db-id必填数据库ID(正整数,从搜索结果中获取)-
sql必填待执行的SQL语句(1-10000个字符)-
logic可选是否使用逻辑库模式false
force可选确认写操作(INSERT/UPDATE/DELETE)false
dry-run可选预览写操作而不实际执行false

Core Workflow

核心工作流

Task 1: Search Target Database

任务1:搜索目标数据库

Search for databases by keyword to get the Database ID:
bash
./scripts/search_database.sh <keyword> --json
Example:
bash
undefined
按关键词搜索数据库获取数据库ID:
bash
./scripts/search_database.sh <keyword> --json
示例:
bash
undefined

Search for databases containing "mydb"

搜索包含"mydb"的数据库

./scripts/search_database.sh mydb --json

The output includes `database_id`, `schema_name`, `db_type`, `host`, `port`, etc.
./scripts/search_database.sh mydb --json

输出包含`database_id`、`schema_name`、`db_type`、`host`、`port`等信息。

Task 2: Execute SQL Query

任务2:执行SQL查询

Execute SQL using the Database ID obtained in the previous step:
bash
./scripts/execute_query.sh --db-id <database_id> --sql "<SQL_statement>"
使用上一步获取的数据库ID执行SQL:
bash
./scripts/execute_query.sh --db-id <database_id> --sql "<SQL_statement>"

Write Operation Protection

写操作保护

For write operations (INSERT/UPDATE/DELETE), the script implements protective pre-check:
ParameterDescription
--force
Required to confirm and execute write operations
--dry-run
Preview write operations without executing
DDL Operations (DROP/TRUNCATE/ALTER/RENAME) are completely blocked — these must be executed via DMS Console.
Examples:
bash
undefined
对于写操作(INSERT/UPDATE/DELETE),脚本实现了保护性预检查:
参数描述
--force
确认并执行写操作时必填
--dry-run
预览写操作而不实际执行
DDL操作(DROP/TRUNCATE/ALTER/RENAME)会被完全拦截 — 此类操作必须通过DMS控制台执行。
示例:
bash
undefined

Read operations (no confirmation needed)

读操作(无需确认)

./scripts/execute_query.sh --db-id 78059000 --sql "SHOW TABLES" ./scripts/execute_query.sh --db-id 78059000 --sql "SELECT * FROM users LIMIT 10" --json
./scripts/execute_query.sh --db-id 78059000 --sql "SHOW TABLES" ./scripts/execute_query.sh --db-id 78059000 --sql "SELECT * FROM users LIMIT 10" --json

Write operations - preview first (recommended)

写操作 - 建议先预览

./scripts/execute_query.sh --db-id 78059000 --sql "INSERT INTO users (name) VALUES ('test')" --dry-run
./scripts/execute_query.sh --db-id 78059000 --sql "INSERT INTO users (name) VALUES ('test')" --dry-run

Write operations - execute with confirmation

写操作 - 确认后执行

./scripts/execute_query.sh --db-id 78059000 --sql "INSERT INTO users (name) VALUES ('test')" --force ./scripts/execute_query.sh --db-id 78059000 --sql "UPDATE users SET name='test' WHERE id=1" --force ./scripts/execute_query.sh --db-id 78059000 --sql "DELETE FROM users WHERE id=1" --force
./scripts/execute_query.sh --db-id 78059000 --sql "INSERT INTO users (name) VALUES ('test')" --force ./scripts/execute_query.sh --db-id 78059000 --sql "UPDATE users SET name='test' WHERE id=1" --force ./scripts/execute_query.sh --db-id 78059000 --sql "DELETE FROM users WHERE id=1" --force

Logic database mode

逻辑库模式

./scripts/execute_query.sh --db-id 78059000 --sql "SELECT 1" --logic
undefined
./scripts/execute_query.sh --db-id 78059000 --sql "SELECT 1" --logic
undefined

Complete Example

完整示例

bash
undefined
bash
undefined

1. Search database (assuming searching for "order")

1. 搜索数据库(假设搜索"order")

./scripts/search_database.sh order --json
./scripts/search_database.sh order --json

Example output:

示例输出:

[{"DatabaseId": "78059000", "SchemaName": "order_db", ...}]

[{"DatabaseId": "78059000", "SchemaName": "order_db", ...}]

2. Execute query

2. 执行查询

./scripts/execute_query.sh --db-id 78059000 --sql "SELECT COUNT(*) FROM orders"
undefined
./scripts/execute_query.sh --db-id 78059000 --sql "SELECT COUNT(*) FROM orders"
undefined

Success Verification

成功校验

After executing SQL, check the returned results:
  1. Script return code is 0
  2. Output contains query results (column names and row data)
  3. No error messages
bash
undefined
执行SQL后,检查返回结果:
  1. 脚本返回码为0
  2. 输出包含查询结果(列名和行数据)
  3. 无错误信息
bash
undefined

Verify query success

校验查询成功

./scripts/execute_query.sh --db-id <db-id> --sql "SELECT 1" --json
./scripts/execute_query.sh --db-id <db-id> --sql "SELECT 1" --json

Expected output: [{"Success": true, "RowCount": 1, ...}]

预期输出:[{"Success": true, "RowCount": 1, ...}]

undefined
undefined

Cleanup

清理

This skill performs read and write operations but does not create persistent resources. No cleanup is required.
本技能执行读写操作,但不会创建持久化资源,无需清理。

Write Operation Safety

写操作安全

Operation TypeBehavior
SELECT / SHOW / DESCExecute directly
INSERT / UPDATE / DELETERequire
--force
or
--dry-run
DROP / TRUNCATE / ALTER / RENAMEBlocked — use DMS Console
操作类型行为
SELECT / SHOW / DESC直接执行
INSERT / UPDATE / DELETE需要
--force
--dry-run
参数
DROP / TRUNCATE / ALTER / RENAME已拦截 — 请使用DMS控制台执行

Available Scripts

可用脚本

ScriptDescription
scripts/search_database.sh
Search databases by keyword
scripts/execute_query.sh
Execute SQL queries
Note: Scripts use aliyun-cli credentials configured via
aliyun configure
.
脚本描述
scripts/search_database.sh
按关键词搜索数据库
scripts/execute_query.sh
执行SQL查询
注意: 脚本使用通过
aliyun configure
配置的aliyun-cli凭证。

Best Practices

最佳实践

  1. Confirm database — Verify the target database before executing SQL
  2. Use --json parameter — Facilitates programmatic processing of output
  3. Preview write operations — Always use
    --dry-run
    first for INSERT/UPDATE/DELETE
  4. Explicit confirmation — Use
    --force
    only after reviewing the preview
  5. Avoid DDL operations — DROP/TRUNCATE/ALTER/RENAME are blocked; use DMS Console instead
  1. 确认数据库 — 执行SQL前校验目标数据库
  2. 使用--json参数 — 方便对输出进行程序化处理
  3. 预览写操作 — 执行INSERT/UPDATE/DELETE前始终先使用
    --dry-run
    预览
  4. 显式确认 — 仅在预览确认后使用
    --force
    参数
  5. 避免DDL操作 — DROP/TRUNCATE/ALTER/RENAME已被拦截,请使用DMS控制台执行

Reference Links

参考链接

DocumentDescription
references/cli-installation-guide.mdCLI Installation Guide
references/ram-policies.mdRAM Permission Policies
references/related-apis.mdRelated API List
references/acceptance-criteria.mdAcceptance Criteria
文档描述
references/cli-installation-guide.mdCLI安装指引
references/ram-policies.mdRAM权限策略
references/related-apis.md相关API列表
references/acceptance-criteria.md验收标准