You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2024.07.16 18:18:42首次发布时间:2023.03.08 19:03:29

本文提供的云数据库 MySQL 版 Python SDK 的代码示例仅供参考。

前提条件

请求超时时间设置

使用 SDK 调用 MySQL 接口时,默认无超时时间限制。您可以在发起接口调用请求时,在代码的最后加上 _request_timeout 参数指定请求的超时时间(单位:秒),若未在该时间内完成,请求会立即终止并报超时错误。例如,您可以在调用 DescribeDBInstances 接口时,通过以下代码设置超时时间为 10 秒。

resp = api_instance.describe_db_instance(
    volcenginesdkrdsmysqlv2.DescribeDBInstanceRequest(
        instance_id="mysql-",
        _configuration=configuration,
    ),
    _request_timeout=10
)

创建 MySQL 实例

关于下述 SDK 代码中使用的参数的定义、取值范围等信息,请参见 CreateDBInstance

from pprint import pprint
import volcenginesdkrdsmysqlv2
import volcenginesdkcore
from volcenginesdkcore.rest import ApiException

if __name__ == "__main__":
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    
    try:
        api_instance = volcenginesdkrdsmysqlv2.RDSMYSQLV2Api(volcenginesdkcore.ApiClient(configuration))
        resp = api_instance.create_db_instance(volcenginesdkrdsmysqlv2.CreateDBInstanceRequest(
            db_engine_version="MySQL_5_7",
            node_info=[
                {
                    "NodeType": "Primary",
                    "ZoneId": "cn-beijing-a",
                    "NodeSpec": "rds.mysql.1c2g",
                },
                {
                    "NodeType": "Secondary",
                    "ZoneId": "cn-beijing-a",
                    "NodeSpec": "rds.mysql.1c2g",
                },
            ],
            storage_type="LocalSSD",
            storage_space=100,
            vpc_id="vpc-****",
            subnet_id="subnet-****",
            charge_info={
                "ChargeType": "PostPaid"
            },
            _configuration=configuration,
        ))
        pprint(resp)
    except ApiException as e:
        print("Exception: %s\n" % e)

本示例中的参数说明如下表所示。

配置项说明取值
DBEngineVersion实例名称。Your Instance Name

NodeInfo

实例规格配置。

[
{
"NodeType": "Primary",
"ZoneId": "cn-beijing-a",
"NodeSpec": "rds.mysql.1c2g"
},
{
"NodeType": "Secondary",
"ZoneId": "cn-beijing-a",
"NodeSpec": "rds.mysql.1c2g"
},
{
"NodeType": "ReadOnly",
"ZoneId": "cn-beijing-a",
"NodeSpec": "rds.mysql.1c2g"
}
]

StorageType实例存储类型。LocalSSD
StorageSpace实例存储空间。100
VpcId专有网络(VPC) ID。vpc-****
SubnetId子网 ID。Subnet123456
InstanceName实例名称。Your Instance Name
SuperAccountName高权限账号名称。Test1
SuperAccountPassword高权限账号的密码。Test@123456
LowerCaseTableNames表名是否区分大小写。1
DBTimeZone时区。UTC +08:00
DBParamGroupId参数模板 ID。ParamGroup123456
ProjectName实例所属项目。Test

ChargeInfo

计费类型。

{
"ChargeType": "PostPaid"
}

InstanceTags

实例标签。

{
"Key": ""aa,
"Value":"aa"
}

Number实例购买数量。1

查询 MySQL 实例信息

from pprint import pprint
import volcenginesdkrdsmysqlv2
import volcenginesdkcore
from volcenginesdkcore.rest import ApiException

if __name__ == "__main__":
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "Your AK"
    configuration.sk = "Your SK"
    configuration.region = "Your Region"
    
    try:
        # 创建 RDS MySQL SDK 客户端,接口调用均需要通过此客户端执行
        api_instance = volcenginesdkrdsmysqlv2.RDSMYSQLV2Api(volcenginesdkcore.ApiClient(configuration))
        resp = api_instance.describe_db_instance_detail(
            volcenginesdkrdsmysqlv2.DescribeDBInstanceDetailRequest(
                instance_id="Your InstanceId",
            )
        )
        print(resp)
    except ApiException as e:
        print("Exception: %s\n" % e)