This topic describes how to use Alibaba Cloud SDK for Python to call the DescribeDBClusters operation of AnalyticDB for MySQL.
View the API documentation
Before you call an API operation, we recommend that you read the documentation about the API operation. For more information, see List of operations by function for Data Warehouse Edition and List of operations by function for Data Lakehouse Edition.
Create a RAM user and grant permissions to the RAM user
If you have created a Resource Access Management (RAM) user and granted permissions to the RAM user, skip this step.
Perform the following steps to create a RAM user:
Log on to the RAM console, go to the Users page, and then click Create User.
Set the Logon Name parameter to adb-openapi-operator and select OpenAPI Access for Access Mode.
Click OK. After the RAM user is created, save the AccessKey ID and AccessKey secret of the user.
Perform the following steps to grant permissions to the RAM user:
Go to the Users page, find the RAM user that you created, and then click Add Permissions in the Actions column.
In the Select Policy section, enter
AliyunADB
in the text box and select the AliyunADBReadOnlyAccess policy.NoteThe AliyunADBReadOnlyAccess policy has permissions to query AnalyticDB for MySQL clusters. You can also select the AliyunADBFullAccess policy that has permissions to query and modify AnalyticDB for MySQL clusters or customize a policy. For more information, see Create a custom policy.
Click OK.
Call the API operation
In this example, Alibaba Cloud SDK for Python is used to call the API operation. You can use SDKs for other languages in a similar manner. For more information, see AnalyticDB for MySQL SDK.
Prepare a Python environment
Download and install Python 3. If you have installed Python, you can run the python --version
command to query the Python version.
Configure environment variables
This section describes how to configure the ALIBABA_CLOUD_ACCESS_KEY_ID
and ALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables in your operating system.
If you use the Linux or macOS operating system, replace
<access_key_id>
and<access_key_secret>
in the following commands with your AccessKey ID and AccessKey secret. Then, run the commands on your device.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
If you use the Windows operating system, create a file to add the
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables, specify your AccessKey ID and AccessKey secret by using the environment variables, and then restart the system.
Install dependencies
Run the following commands on your device to install environment dependencies.
pip install alibabacloud_credentials
pip install alibabacloud_adb20190315
pip install alibabacloud_tea_console
Download the sample code
Go to the DescribeDBClusters page.
On the left-side Parameters tab, specify the required parameters. In this example, specify cn-hangzhou for the RegionId parameter.
On the right-side SDK Sample Code tab, select Python for the language and click Download Project to download the sample code package.
Decompress the sample code package on your device and go to the alibabacloud_sample directory.
Run the code
Run the following command:
python sample.py
Sample result:
[
LOG
] {
"headers":{
"date":"Tue, 01 Aug 2023 02:05:31 GMT",
"content-type":"application/json;charset=utf-8",
"content-length":"1042","connection":"keep-alive",
"keep-alive":"timeout=25","vary":"Accept-Encoding",
"access-control-allow-origin":"*",
"access-control-expose-headers":"*",
"x-acs-request-id":"A239D19A-3862-52A9-8D85-09E65F07****",
"x-acs-trace-id":"a1f567557dc98fb5f368346302e6****",
"etag":"1uiSaYPuCXwyGD9ECHaa****"
},
"statusCode":200,
"body":{
"Items":{
"DBCluster":[
{
"Category":"MIXED_STORAGE",
"CommodityCode":"ads",
"ComputeResource":"32Core128GBNEW",
"ConnectionString":"am-bp1255hwaziiskcnu9****.ads.aliyuncs.com",
"CreateTime":"2023-07-31T05:40:36Z",
"DBClusterDescription":"am-bp1255hwaziis****",
"DBClusterId":"am-bp1255hwaziis****",
"DBClusterNetworkType":"VPC",
"DBClusterStatus":"Running",
"DBClusterType":"Common",
"DBNodeClass":"E32",
"DBNodeCount":1,
"DBNodeStorage":100,
"DBVersion":"3.0",
"DiskType":"cloud_essd",
"DtsJobId":"",
"ElasticIOResource":1,
"Engine":"AnalyticDB",
"ExecutorCount":2,
"LockMode":"Unlock",
"LockReason":"",
"Mode":"flexible",
"PayType":"Postpaid",
"Port":"3306",
"RdsInstanceId":"",
"RegionId":"cn-hangzhou",
"ResourceGroupId":"rg-acfmz7u4zzr****",
"StorageResource":"24Core192GB",
"Tags":{"Tag":[]},
"VPCCloudInstanceId":"am-bp1255hwaziis****-controller",
"VPCId":"vpc-bp1ov7as4yvz4kxei****",
"VSwitchId":"vsw-bp14jupk2fa0avw8v****",
"ZoneId":"cn-hangzhou-h"
}
]
},
"PageNumber":1,
"PageSize":30,
"RequestId":"A239D19A-3862-52A9-8D85-09E65F07B98C","TotalCount":1
}
}