本文介绍 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})
关于下述 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 | 实例规格配置。 | [ |
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 | 计费类型。 | { |
InstanceTags | 实例标签。 | { |
Number | 实例购买数量。 | 1 |
关于下述 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) }