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

本文介绍 MySQL Go SDK 的代码示例仅供您参考。

前提条件

请求超时时间设置

使用 Go SDK 调用 MySQL 接口时,默认无超时时间设置。 您可以在初始化客户端时,通过 WithHTTPClient(&http.Client{Timeout: <超时时间> * time.Second}) 来指定客户端的请求超时时间,若未在该时间内完成,请求会立即终止并报超时错误。例如,您可以通过以下代码将客户端的请求超时时间设置为 10 秒。

config = volcengine.NewConfig().
    WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).
    WithRegion(region).
    WithHTTPClient(&http.Client{Timeout: 10 * time.Second})

创建 MySQL 实例

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

package main

import (
    "fmt"
    "os"

    "github.com/volcengine/volcengine-go-sdk/service/rdsmysqlv2"
    "github.com/volcengine/volcengine-go-sdk/volcengine"
    "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
    "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
    var (
        ak     string
        sk     string
        region string
        config *volcengine.Config
        sess   *session.Session
        client *rdsmysqlv2.RDSMYSQLV2
        resp   *rdsmysqlv2.CreateDBInstanceOutput
        err    error
    )

    ak = "Your AK"
    sk = "Your SK"
    region = "Your Region"
    config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).WithRegion(region)
    sess, err = session.NewSession(config)
    if err != nil {
        fmt.Printf("Failed to create session, err: %v\n", err)
        os.Exit(1)
    }

    client = rdsmysqlv2.New(sess)

    newStr := func(s string) *string {
        return &s
    }

    newInt := func(i int32) *int32 {
        return &i
    }

    resp, err = client.CreateDBInstance(&rdsmysqlv2.CreateDBInstanceInput{
        DBEngineVersion: newStr("MySQL_Community_5_7"),
        StorageType:     newStr("LocalSSD"),
        VpcId:           newStr("vpc-*"),
        SubnetId:        newStr("subnet-*"),
        StorageSpace:    newInt(20),
        NodeInfo: []*rdsmysqlv2.NodeInfoForCreateDBInstanceInput{
            {
                NodeType: newStr("Primary"),
                ZoneId:   newStr("cn-beijing-a"),
                NodeSpec: newStr("rds.mysql.1c2g"),
            },
            {
                NodeType: newStr("Secondary"),
                ZoneId:   newStr("cn-beijing-a"),
                NodeSpec: newStr("rds.mysql.1c2g"),
            },
        },
        ChargeInfo: &rdsmysqlv2.ChargeInfoForCreateDBInstanceInput{
            ChargeType: newStr("PostPaid"),
        },
    })

    if err != nil {
        fmt.Printf("Error when calling RDSMYSQLApi->CreateDBInstance, err: %v\n", err)
        os.Exit(1)
    }

    fmt.Println(resp)
}

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

配置项说明取值
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 实例信息

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

package main

import (
    "fmt"
    "os"

    "github.com/volcengine/volcengine-go-sdk/service/rdsmysqlv2"
    "github.com/volcengine/volcengine-go-sdk/volcengine"
    "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
    "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
    var (
        ak     string
        sk     string
        region string
        config *volcengine.Config
        sess   *session.Session
        client *rdsmysqlv2.RDSMYSQLV2
        resp   *rdsmysqlv2.DescribeDBInstanceDetailOutput
        err    error
    )

    ak = "Your AK"
    sk = "Your SK"
    region = "Your Region"
    config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).WithRegion(region)
    sess, err = session.NewSession(config)
    if err != nil {
        fmt.Printf("Failed to create session, err: %v\n", err)
        os.Exit(1)
    }

    client = rdsmysqlv2.New(sess)

    newStr := func(s string) *string {
        return &s
    }

    resp, err = client.DescribeDBInstanceDetail(&rdsmysqlv2.DescribeDBInstanceDetailInput{
        InstanceId: newStr("Your InstanceId"),
    })

    if err != nil {
        fmt.Printf("Error when calling RDSMYSQLApi->DescribeDBInstanceDetail, err: %v\n", err)
        os.Exit(1)
    }

    fmt.Println(resp)
}