Relational Database (MySQL)
Since v5.0.0, this module has been added. Accessed via app.mysql, it provides full management capabilities for TCB-hosted MySQL (Serverless CynosDB) databases.
Initialize
import CloudBase from '@cloudbase/manager-node'
const app = CloudBase.init({
secretId: 'Your SecretId',
secretKey: 'Your SecretKey',
envId: 'Your envId'
})
const mysql = app.mysql
createMySQL
1. API Description
API feature: Activate the MySQL database for the current environment.
API declaration: app.mysql.createMySQL(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| DbInstanceType | No | String | Database type, fixed value MYSQL |
| MysqlVersion | No | String | MySQL version, such as 5.7 |
| VpcId | No | String | VPC ID |
| SubnetId | No | String | Subnet ID |
| LowerCaseTableNames | No | String | Table name case sensitivity: 0 (case-sensitive) / 1 (case-insensitive, default) |
3. Return Results
| Field | Type | Description |
|---|---|---|
| RequestId | String | Unique identifier of the request |
| Data.TaskId | String | Asynchronous provisioning task ID |
4. Sample Code
const { Data } = await mysql.createMySQL({ MysqlVersion: '5.7' })
console.log('Provisioning task ID:', Data.TaskId)
describeCreateResult
1. API Description
API feature: Query MySQL provisioning status
API declaration: app.mysql.describeCreateResult(options?): Promise<Object>
2. Return Results
| Field | Type | Description |
|---|---|---|
| Data.Status | String | notexist (Not activated) / success (Activated) |
| Data.FailReason | String | Failure reason (present when failed) |
| Data.FreezeStatus | Boolean | Whether it is frozen |
describeClusterDetail
1. API Description
API feature: Query MySQL cluster details (database information, network configuration, etc.)
API declaration: app.mysql.describeClusterDetail(): Promise<Object>
2. Return Results
| Field | Type | Description |
|---|---|---|
| Data.DbClusterId | String | Cluster ID |
| Data.DbInfo | Object | Database status, version, storage, and other information |
| Data.NetInfo | Object | Internal and external IP addresses, VPC, and other network information |
destroyMySQL
1. API Description
API feature: Destroy the MySQL instance in the current environment.
API declaration: app.mysql.destroyMySQL(): Promise<Object>
2. Return Results
| Field | Type | Description |
|---|---|---|
| Data.IsSuccess | Boolean | whether the operation was successful |
| Data.TaskId | String | Task ID |
| Data.TaskName | String | Task Name |
describeTaskStatus
1. API Description
API feature: Query MySQL asynchronous task status
API declaration: app.mysql.describeTaskStatus(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| TaskId | No | String | Task ID |
3. Return Results
| Field | Type | Description |
|---|---|---|
| Status | String | Task status, such as SUCCESS / FAILED |
| StatusDesc | String | Status description |
runSql
1. API Description
API feature: Directly execute SQL statements (SELECT/INSERT/UPDATE/DELETE, etc.)
API declaration: app.mysql.runSql(options): Promise<IRunSqlResult>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Sql | Required | String | SQL statement to be executed |
| DbInstance | No | Object | Database connector instance information |
| ReadOnly | No | Boolean | Read-only mode (only allows SELECT/WITH/SHOW/DESCRIBE/EXPLAIN statements) |
3. Return Results
| Field | Type | Description |
|---|---|---|
| Items | String[] | Query result rows (each row is a JSON string) |
| Infos | String[] | Column metadata |
| RowsAffected | Number | Number of affected rows |
| RequestId | String | Unique identifier of the request |
4. Sample Code
const res = await mysql.runSql({
Sql: 'SELECT * FROM users WHERE id = 1'
})
const rows = res.Items.map(item => JSON.parse(item))
console.log(rows)
openWan / closeWan
1. API Description
API feature: Enable/disable public network access for instances.
API Declaration:
app.mysql.openWan(options?): Promise<Object>app.mysql.closeWan(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| InstanceId | No | String | Instance ID |
| InstanceGroupId | No | String | Instance Group ID |
3. Return Results
| Field | Type | Description |
|---|---|---|
| FlowId | Number | Workflow ID |
| RequestId | String | Unique identifier of the request |
describeClusterInstanceGroups
1. API Description
API feature: Query cluster instance group list (including network information and instance list of each instance group)
API declaration: app.mysql.describeClusterInstanceGroups(): Promise<Object>
2. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count of instance groups |
| InstanceGroupInfoList | Array | List of instance groups, each containing Vip/Vport/InstanceSet, etc. |
describeInstances
1. API Description
API feature: Query the instance list, supporting multi-dimensional filtering
API declaration: app.mysql.describeInstances(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Limit | No | Number | Number of returned items, default 20, value range (0,100] |
| Offset | No | Number | Offset, default 0 |
| OrderBy | No | String | Sorting field: CREATETIME / PERIODENDTIME |
| OrderByType | No | String | Sort order: ASC / DESC |
| Filters | No | IQueryFilter[] | Filter conditions with an AND relationship between them |
| Status | No | String | Instance status: creating / running / isolating / isolated, etc. |
| InstanceIds | No | String[] | Filter by instance ID list |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count of instances |
| InstanceSet | Array | List of instance details |
describeInstanceDetail
1. API Description
API feature: Querying the full details of a single instance
API declaration: app.mysql.describeInstanceDetail(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| InstanceId | Yes | String | Instance ID |
3. Return Results
Returns a Detail object containing full details of the instance, such as CPU/memory/storage, network, status, version, etc. (see ICynosdbInstanceFullDetail).
restartInstance
1. API Description
API feature: Restarting the specified instance
API declaration: app.mysql.restartInstance(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| InstanceId | Yes | String | Instance ID |
3. Return Results
| Field | Type | Description |
|---|---|---|
| FlowId | Number | Asynchronous Workflow ID |
| RequestId | String | Unique identifier of the request |
upgradeInstance
1. API Description
API feature: Modify instance specifications (CPU / Memory)
API declaration: app.mysql.upgradeInstance(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| InstanceId | Yes | String | Instance ID |
| Cpu | Yes | Number | Target CPU core count |
| Memory | Yes | Number | Target memory (GB) |
| UpgradeType | Yes | String | Upgrade timing: upgradeImmediate (immediate) / upgradeInMaintain (maintenance window) |
| UpgradeMode | No | String | Upgrade mode: NormalUpgrade (normal) / FastUpgrade (fast) |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TranId | String | Freeze transaction ID |
| BigDealIds | String[] | big order IDs |
| DealNames | String[] | order IDs |
describeInstanceSlowQueries
1. API Description
API feature: Querying the slow query logs of an instance
API declaration: app.mysql.describeInstanceSlowQueries(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| InstanceId | Yes | String | Instance ID |
| StartTime | No | String | Start Time |
| EndTime | No | String | End Time |
| Limit | Optional | Number | Returned quantity |
| Offset | No | Number | offset |
| Username | Optional | String | Filter by username |
| Host | Optional | String | Filter by client Host |
| Database | Optional | String | Filter by database name |
| OrderBy | No | String | Sorting field: QueryTime / LockTime / RowsExamined / RowsSent |
| OrderByType | No | String | Sort order: asc / desc |
| SqlText | Optional | String | Filter SQL statements by keywords |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count |
| SlowQueries | Array | List of slow query records (including SQL statements, execution time, rows scanned, etc.) |
describeInstanceErrorLogs
1. API Description
API feature: Querying the error logs of an instance
API declaration: app.mysql.describeInstanceErrorLogs(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| InstanceId | Required | String | Instance ID |
| StartTime | No | String | Start Time |
| EndTime | No | String | End Time |
| Limit | Optional | Number | Returned quantity |
| Offset | No | Number | offset |
| OrderBy | Optional | String | Sorting field, supporting Timestamp |
| OrderByType | No | String | Sort order: ASC / DESC |
| LogLevels | Optional | String[] | Log Level: error / warning / note |
| KeyWords | Optional | String[] | Keywords filtering (fuzzy search) |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count |
| ErrorLogs | Array | Error logs list (containing Content/Level/Timestamp) |
describeClusterDatabases
1. API Description
API feature: Query database list in the cluster
API declaration: app.mysql.describeClusterDatabases(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Offset | No | Number | offset |
| Limit | Optional | Number | Returned quantity |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count |
| Databases | String[] | Database name list |
describeClusterDatabaseTables
1. API Description
API feature: Query table list in the specified database
API declaration: app.mysql.describeClusterDatabaseTables(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Db | Yes | String | Database name |
| Offset | No | Number | offset |
| Limit | Optional | Number | Number of returned items |
| TableType | No | String | Table type: view (view) / base_table (base table) / all (all, default) |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count |
| Tables | String[] | List of table names |
4. Sample Code
const { Databases } = await mysql.describeClusterDatabases()
for (const db of Databases) {
const { Tables } = await mysql.describeClusterDatabaseTables({ Db: db })
console.log(`${db}: ${Tables.join(', ')}`)
}
createAccounts
1. API Description
API feature: Batch create database accounts.
API declaration: app.mysql.createAccounts(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Accounts | Required | INewAccount[] | New account list |
INewAccount
| Field | Required | Type | Description |
|---|---|---|---|
| AccountName | Required | String | Account name |
| AccountPassword | Required | String | Password (8-64 characters, including uppercase letters, lowercase letters, digits, and symbols) |
| Host | Required | String | Allowed host (IP or %) |
| Description | No | String | Account description |
| MaxUserConnections | No | Number | Maximum connection limit |
deleteAccounts
1. API Description
API feature: Batch delete database accounts
API declaration: app.mysql.deleteAccounts(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Accounts | Optional | IInputAccount[] | List of accounts to be deleted ({ AccountName, Host? }) |
describeAccounts
1. API Description
API feature: Query database account list
API declaration: app.mysql.describeAccounts(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| AccountNames | Optional | String[] | Filter by account name |
| Hosts | Optional | String[] | Filter by Host |
| Limit | Optional | Number | Returned quantity |
| Offset | Optional | Number | Offset |
| AccountRegular | Optional | String | Fuzzy match keywords (supports regular expressions) |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count of accounts |
| AccountSet | Array | Account information list (including AccountName/Host/CreateTime, etc.) |
describeAccountPrivileges
1. API Description
API feature: Query account permissions on specified databases/tables
API declaration: app.mysql.describeAccountPrivileges(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| AccountName | Required | String | Account name |
| Host | Required | String | Host |
| Db | Required | String | Database name, * indicates global privileges |
| Type | Required | String | Object type: table (table level) / * (database/global level) |
| TableName | No | String | Table name (Required when Type='table') |
3. Return Results
| Field | Type | Description |
|---|---|---|
| Privileges | String[] | Permission list |
describeAccountAllGrantPrivileges
1. API Description
API feature: Query all granted permissions for the account (global, database, table)
API declaration: app.mysql.describeAccountAllGrantPrivileges(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Account | Required | IInputAccount | { AccountName, Host? } |
3. Return Results
| Field | Type | Description |
|---|---|---|
| PrivilegeStatements | String[] | Privilege statements |
| GlobalPrivileges | String[] | Global privileges list |
| DatabasePrivileges | Array | Database privileges |
| TablePrivileges | Array | Table privileges |
modifyAccountPrivileges
1. API Description
API feature: Modify account permissions
API declaration: app.mysql.modifyAccountPrivileges(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Account | Required | IInputAccount | Account information |
| GlobalPrivileges | Optional | String[] | Global privileges list |
| DatabasePrivileges | Optional | Array | Database-level privileges |
| TablePrivileges | Optional | Array | Table-level privileges |
modifyAccountDescription
API declaration: app.mysql.modifyAccountDescription(options): Promise<Object>
Modify account description information. Parameters: AccountName (Required), Description (Required), Host (Optional).
modifyAccountHost
API declaration: app.mysql.modifyAccountHost(options): Promise<Object>
Modify the Host allowed for account access. Parameters: Account (IInputAccount, Required), NewHost (New Host, Required).
modifyAccountParams
API declaration: app.mysql.modifyAccountParams(options): Promise<Object>
Modify account configuration parameters. Parameters: Account (Required), AccountParams (IAccountParamItem[], Required, each item contains ParamName and ParamValue).
resetAccountPassword
1. API Description
API feature: Reset account password
API declaration: app.mysql.resetAccountPassword(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| AccountName | Required | String | Account name |
| AccountPassword | Required | String | New password |
| Host | Optional | String | Host, default % |
createBackup
1. API Description
API feature: Manually create database backups.
API declaration: app.mysql.createBackup(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| BackupType | Optional | String | Backup type: logic (logical) / snapshot (physical, default) |
| BackupDatabases | Optional | String[] | Databases to back up (only valid for logic type) |
| BackupTables | Optional | Array | Tables to back up (only valid for logic type) |
| BackupName | Optional | String | Backup remark name |
3. Return Results
| Field | Type | Description |
|---|---|---|
| FlowId | Number | Backup Workflow ID |
deleteBackup
1. API Description
API feature: Delete backup files
API declaration: app.mysql.deleteBackup(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| BackupIds | Optional | Number[] | Backup ID list (recommended) |
| SnapshotIdList | Optional | Number[] | Snapshot ID list (legacy, not recommended) |
modifyBackupConfig
1. API Description
API feature: Modify backup policy configuration (backup time window, retention period, backup frequency)
API declaration: app.mysql.modifyBackupConfig(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| BackupTimeBeg | No | Number | Full backup start time (seconds, e.g. 0=0:00, 3600=1:00) |
| BackupTimeEnd | Optional | Number | Full backup end time |
| ReserveDuration | Optional | Number | Backup retention period (in seconds, e.g., seven days=604800) |
| BackupFreq | Optional | String[] | Array of length 7, corresponding to Monday to Sunday, full/increment |
| BackupType | Optional | String | Backup type |
describeBackupList
1. API Description
API feature: Query backup list
API declaration: app.mysql.describeBackupList(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| Limit | Optional | Number | Number of returned items, value range (0,100] |
| Offset | Optional | Number | Offset |
| BackupType | Optional | String | snapshot (snapshot) / logic (logical) |
| BackupMethod | Optional | String | auto (automatic) / manual (manual) |
| StartTime | Optional | String | Filter by backup start time |
| EndTime | Optional | String | Filter by backup end time |
| BackupNames | Optional | String[] | Filter by remark name (fuzzy) |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count |
| BackupList | Array | List of backup files (including BackupId/BackupType/BackupStatus/StartTime/FinishTime etc.) |
describeBackupDownloadUrl
1. API Description
API feature: Obtain the download address of backup files
API declaration: app.mysql.describeBackupDownloadUrl(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| BackupId | Required | Number | Backup ID |
3. Return Results
| Field | Type | Description |
|---|---|---|
| DownloadUrl | String | Backup download URL |
4. Sample Code
const { BackupList } = await mysql.describeBackupList({ Limit: 5 })
const latest = BackupList[0]
const { DownloadUrl } = await mysql.describeBackupDownloadUrl({ BackupId: latest.BackupId })
console.log('Download URL:', DownloadUrl)
rollBackCluster
1. API Description
API feature: Restore the cluster to a specified time point or backup file.
API declaration: app.mysql.rollBackCluster(options): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| RollbackStrategy | Required | String | Rollback policy: timeRollback (by point in time) / snapRollback (by backup file) |
| RollbackId | No | Number | Backup file ID (required when snapRollback) |
| ExpectTime | No | String | Expected time point (Required when timeRollback) |
| RollbackDatabases | Optional | Array | List of rollback databases (including OldDatabase / NewDatabase) |
| RollbackTables | Optional | Array | Rollback table list (including Database and Tables) |
| RollbackMode | Optional | String | Point-in-time rollback mode: full (normal) / db (fast) / table (extreme-speed) |
3. Return Results
| Field | Type | Description |
|---|---|---|
| FlowId | Number | Rollback Workflow ID |
| RequestId | String | Unique identifier of the request |
4. Sample Code
// Roll back to a point in time (roll back the users table to a new table users_restore)
const { FlowId } = await mysql.rollBackCluster({
RollbackStrategy: 'timeRollback',
ExpectTime: '2025-01-01 12:00:00',
RollbackTables: [{
Database: 'mydb',
Tables: [{ OldTable: 'users', NewTable: 'users_restore' }]
}],
RollbackMode: 'table'
})
console.log('Rollback workflow ID:', FlowId)
describeTasks
1. API Description
API feature: List MySQL asynchronous tasks
API declaration: app.mysql.describeTasks(options?): Promise<Object>
2. Input Parameters
| Field | Required | Type | Description |
|---|---|---|---|
| StartTimeBegin | No | String | Lower bound of task start time |
| StartTimeEnd | No | String | Upper bound of task start time |
| Filters | No | IQueryFilter[] | Filter conditions |
| Limit | Optional | Number | Returned quantity |
| Offset | No | Number | offset |
3. Return Results
| Field | Type | Description |
|---|---|---|
| TotalCount | Number | Total count |
| TaskList | Array | Task list (including ID/Status/TaskType/StartTime, etc.) |