使用指南
云数据库 GaiaDB Go SDK 使用指南
目录
初始化客户端
GaiaDB 服务的默认 Endpoint 为 gaiadb.bj.baidubce.com(北京地域)。使用前请准备好账号的 Access Key(AK)与 Secret Key(SK)。
1import "github.com/baidubce/bce-sdk-go/services/gaiadb"
2
3// endPoint 传空字符串时使用默认 gaiadb.bj.baidubce.com
4AK, SK, endPoint := "your-ak", "your-sk", "gaiadb.bj.baidubce.com"
5client, err := gaiadb.NewClient(AK, SK, endPoint)
6if err != nil {
7 panic(err)
8}
集群管理
查询可用子网
方法: GetAvailableSubnetList(vpcId string)
1subnets, err := client.GetAvailableSubnetList("vpc-xxxyyy")
2for _, s := range subnets {
3 fmt.Println(s.VpcId, s.SubnetId)
4}
创建集群
方法: CreateCluster(args *CreateClusterArgs)
请求参数(CreateClusterArgs)
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
ClientToken |
string | 是 | 随机唯一串,保证幂等 |
Number |
int | 是 | 创建数量 |
ProductType |
string | 是 | 付费类型:postpay(后付费)/ prepay(预付费) |
InstanceParam.ReleaseVersion |
string | 是 | 数据库版本,如 8.0 |
InstanceParam.AllocatedCpuInCore |
int | 是 | CPU 核数 |
InstanceParam.AllocatedMemoryInMB |
int | 是 | 内存大小(MB) |
InstanceParam.AllocatedStorageInGB |
int | 是 | 存储空间(GB) |
InstanceParam.InstanceAmount |
int | 是 | 实例数量 |
InstanceParam.ProxyAmount |
int | 是 | Proxy 数量 |
InstanceParam.VpcId |
string | 是 | VPC ID |
InstanceParam.SubnetId |
string | 是 | 子网 ID |
1args := &gaiadb.CreateClusterArgs{
2 ClientToken: "随机唯一串,保证幂等",
3 Number: 1,
4 ProductType: "postpay",
5 InstanceParam: gaiadb.InstanceParam{
6 ReleaseVersion: "8.0",
7 AllocatedCpuInCore: 2,
8 AllocatedMemoryInMB: 8192,
9 AllocatedStorageInGB: 5120,
10 InstanceAmount: 2,
11 ProxyAmount: 2,
12 VpcId: "vpc-xxxx",
13 SubnetId: "sbn-xxxx",
14 },
15}
16result, err := client.CreateCluster(args)
17// result.OrderId, result.ClusterIds
其他集群操作
| 方法 | 参数 | 说明 |
|---|---|---|
DeleteCluster |
clusterId | 删除集群 |
RenameCluster |
clusterId, *ClusterName | 集群重命名 |
ResizeCluster |
clusterId, *ResizeClusterArgs | 变配,返回 OrderId |
GetClusterList |
*Marker | 分页查询集群列表 |
GetClusterDetail |
clusterId | 查询集群详情 |
GetClusterCapacity |
clusterId | 查询集群容量 |
QueryClusterPrice |
*QueryPriceArgs | 查询创建价格 |
QueryResizeClusterPrice |
*QueryResizePriceArgs | 查询变配差价 |
RebootInstance |
clusterId, instanceId, *RebootInstanceArgs | 重启实例 |
ClusterSwitch |
clusterId, *ClusterSwitchArgs | 主备切换 |
BindTags |
*BindTagsArgs | 绑定标签 |
GetClusterByVpcId |
vpcId | 按 VPC 查询集群 |
GetClusterByLbId |
lbIds | 按 LB 查询集群 |
连接地址管理
| 方法 | 说明 |
|---|---|
GetInterfaceList(clusterId) |
查询连接地址列表 |
UpdateDnsName(clusterId, *UpdateDnsNameArgs) |
更新 DNS 名称 |
UpdateInterface(clusterId, *UpdateInterfaceArgs) |
更新连接地址配置 |
NewInstanceAutoJoin(clusterId, *NewInstanceAutoJoinArgs) |
新实例自动加入连接地址 |
账号管理
创建账号
方法: CreateAccount(clusterId string, args *CreateAccountArgs)
请求参数(CreateAccountArgs)
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
AccountName |
string | 是 | 账号名称 |
Password |
string | 是 | 账号密码 |
AccountType |
string | 是 | 账号类型,如 common |
Remark |
string | 否 | 备注 |
1err := client.CreateAccount(clusterId, &gaiadb.CreateAccountArgs{
2 AccountName: "baidu",
3 Password: "your-password",
4 AccountType: "common",
5 Remark: "remark",
6})
其他账号操作
| 方法 | 说明 |
|---|---|
DeleteAccount(clusterId, accountName) |
删除账号 |
GetAccountDetail(clusterId, accountName) |
查询账号详情 |
GetAccountList(clusterId) |
查询账号列表 |
UpdateAccountRemark(clusterId, accountName, *args) |
更新账号备注 |
UpdateAccountAuthIp(clusterId, accountName, *args) |
更新账号授权 IP |
UpdateAccountPrivileges(clusterId, accountName, *args) |
更新账号权限 |
UpdateAccountPassword(clusterId, accountName, *args) |
更新账号密码 |
注意: 更新类接口的 args 中需携带从查询接口获取到的
Etag。
数据库管理
| 方法 | 说明 |
|---|---|
CreateDatabase(clusterId, *CreateDatabaseArgs) |
创建数据库 |
DeleteDatabase(clusterId, dbName) |
删除数据库 |
ListDatabase(clusterId) |
查询数据库列表 |
备份与快照
| 方法 | 说明 |
|---|---|
CreateSnapshot(clusterId) |
创建快照 |
ListSnapshot(clusterId) |
查询快照列表 |
UpdateSnapshotPolicy(clusterId, *UpdateSnapshotPolicyArgs) |
更新备份策略 |
GetSnapshotPolicy(clusterId) |
查询备份策略 |
白名单管理
查询白名单
方法: GetWhiteList(clusterId string)
更新白名单
方法: UpdateWhiteList(clusterId string, args *WhiteList)
注意: 更新白名单时需将查询到的
Etag携带回请求,防止并发覆盖。
1// 查询白名单(含 Etag)
2wl, err := client.GetWhiteList(clusterId)
3if err != nil {
4 // 错误处理
5}
6// 更新白名单,需带回 Etag
7err = client.UpdateWhiteList(clusterId, &gaiadb.WhiteList{
8 AuthIps: []string{"10.0.0.0/8"},
9 Etag: wl.Etag,
10})
多活组管理
| 方法 | 说明 |
|---|---|
CreateMultiactiveGroup(*CreateMultiactiveGroupArgs) |
创建多活组 |
DeleteMultiactiveGroup(groupId) |
删除多活组 |
RenameMultiactiveGroup(groupId, *RenameMultiactiveGroupArgs) |
多活组重命名 |
MultiactiveGroupList() |
查询多活组列表 |
MultiactiveGroupDetail(groupId) |
查询多活组详情 |
GetSyncStatus(groupId, followerClusterId) |
查询同步状态 |
GroupExchange(groupId, *ExchangeArgs) |
主从角色切换 |
参数管理
| 方法 | 说明 |
|---|---|
GetParamsList(clusterId) |
查询参数列表 |
GetParamsHistory(clusterId) |
查询参数修改历史 |
UpdateParams(clusterId, *UpdateParamsArgs) |
修改参数,Timing 可指定生效时机 |
UpdateParamsArgs 参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
Timing |
string | 生效时机,如 now(立即)或 maintenance(维护窗口内) |
Params |
ParamsItem | 要修改的参数键值对 |
1err := client.UpdateParams(clusterId, &gaiadb.UpdateParamsArgs{
2 Timing: "now",
3 Params: gaiadb.ParamsItem{"max_connections": "1000"},
4})
参数模板
| 方法 | 说明 |
|---|---|
ListParamTemplate(*ListParamTempArgs) |
查询模板列表(分页) |
CreateParamTemplate(*CreateParamTemplateArgs) |
创建模板 |
SaveAsParamTemplate(*ParamTempArgs) |
另存为模板 |
UpdateParamTemplate(templateId, *UpdateParamTplArgs) |
更新模板信息 |
DeleteParamTemplate(templateId) |
删除模板 |
ModifyParams(templateId, *ModifyParamsArgs) |
修改模板内参数 |
DeleteParamsFromTemp(templateId, *Params) |
从模板删除参数 |
GetParamTemplateDetail(templateId, detail) |
查询模板详情 |
GetParamTemplateHistory(templateId, action) |
查询模板操作历史 |
GetTemplateApplyRecords(templateId) |
查询模板应用记录 |
ApplyParamTemplate(templateId, *ApplyParamTemplateArgs) |
应用模板到集群 |
运维时间窗
| 方法 | 说明 |
|---|---|
GetMaintenTime(clusterId) |
查询运维时间窗 |
UpdateMaintenTime(clusterId, *UpdateMaintenTimeArgs) |
更新运维时间窗 |
UpdateMaintenTimeArgs 参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
Period |
string | 维护周期 |
StartTime |
string | 维护起始时间 |
Duration |
int | 维护时长(分钟) |
慢 SQL
查询慢 SQL 明细
方法: GetSlowSqlDetail(clusterId string, args *GetSlowSqlArgs)
支持分页、时间范围、schema 等筛选条件。
获取慢 SQL 优化建议
方法: SlowSqlAdvice(clusterId string, sqlId string)
返回索引建议和语句优化建议。
Binlog
查询 Binlog 列表
方法: GetBinlogList(args *GetBinlogListArgs)
支持分页、时间范围等筛选。
查询 Binlog 详情
方法: GetBinlogDetail(binlogId string, args *GetBinlogArgs)
返回结果中 OuterLinks 字段包含 Binlog 文件下载外链。
任务与订单
| 方法 | 说明 |
|---|---|
GetTaskList(*TaskListArgs) |
查询任务列表(支持时间范围、地域等筛选) |
ExecuteTaskNow(taskId) |
立即执行任务 |
CancelTask(taskId) |
取消任务 |
GetOrderInfo(orderId) |
查询订单状态 |
错误处理
所有接口均返回 error。调用成功时 error 为 nil,失败时返回 *bce.BceServiceError,包含错误码与请求 ID。
1detail, err := client.GetClusterDetail(clusterId)
2if err != nil {
3 if serviceErr, ok := err.(*bce.BceServiceError); ok {
4 fmt.Println(serviceErr.Code, serviceErr.Message, serviceErr.RequestId)
5 }
6 return
7}
评价此篇文章
