更新时间:2024-12-16 GMT+08:00
分享

开启数据库代理

功能介绍

开启数据库代理,只支持ELB模式。

调用方法

请参见如何调用API

URI

POST /v3/{project_id}/instances/{instance_id}/proxy

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

租户在某一Region下的project ID。

获取方法请参见获取项目ID

instance_id

String

实例ID,严格匹配UUID规则。

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

用户Token。

通过调用IAM服务获取用户Token接口获取。

请求响应成功后在响应消息头中包含的“X-Subject-Token”的值即为Token值。

X-Language

String

请求语言类型。默认en-us。

取值范围:

  • en-us

  • zh-cn

表3 请求Body参数

参数

是否必选

参数类型

描述

flavor_ref

String

数据库代理规格码,获取方法请参见查询数据库代理规格信息返回的spec_code字段。

node_num

Integer

数据库代理节点数,取值整数2-16。

proxy_name

String

数据库代理名称。用于表示实例的名称,同一租户下,同类型的实例名可重名。

取值范围:最小为4个字符,最大为64个字符且不超过64个字节(注意:一个中文字符占用3个字节),必须以字母或中文开头,区分大小写,可以包含字母、数字、中划线、下划线或中文,不能包含其他特殊字符。

proxy_mode

String

数据库代理类型。默认类型为readwrite。

route_mode

Integer

数据库代理路由模式,默认为0,权重负载模式。

取值范围:

  • 0:表示权重负载模式。

  • 1:表示负载均衡模式(数据库主节点不接受读请求)。

  • 2:表示负载均衡模式(数据库主节点接受读请求)。

nodes_read_weight

Array of NodesWeight objects

数据库节点的读权重设置。

在proxy_mode为readonly时,只能为只读节点选择权重。

subnet_id

String

数据库VPC下的子网ID。

new_node_auto_add_status

String

是否开启新增节点自动加入该Proxy。如果需要设置是否开启新增节点自动加入该Proxy,请联系客服人员添加白名单,加入白名单后,方可输入该字段。加入白名单后,默认值为OFF。

取值范围:

  • ON:开启。

  • OFF:关闭。

new_node_weight

Integer

新增节点的读权重,在new_node_auto_add_status参数为ON时为必选字段:

  • 如果路由模式为0,新增节点自动加入为ON,取值为0~1000。

  • 如果路由模式不为0或新增节点自动加入为OFF,则可不输入读权重。

表4 NodesWeight

参数

是否必选

参数类型

描述

id

String

数据库节点ID。

weight

Integer

权重:

  • 如果路由模式为0,取值为0~1000。

  • 如果路由模式为1,数据库主节点取值为0,只读节点取值为0或1。

  • 如果路由模式为2,数据库主节点取值为1,只读节点取值为0或1。

响应参数

状态码: 201

表5 响应Body参数

参数

参数类型

描述

job_id

String

任务ID。

状态码: 400

表6 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息。

状态码: 500

表7 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息。

请求示例

开启数据库代理,规格2U8GB,代理模式只读模式。

POST https://{endpoint}/v3/0483b6b16e954cb88930a360d2c4e663/instances/43e4feaab48f11e89039fa163ebaa7e4br01/proxy

{
  "flavor_ref" : "gaussdb.proxy.large.x86.2",
  "node_num" : 2,
  "proxy_name" : "gaussdb-proxy",
  "proxy_mode" : "readonly",
  "route_mode" : 0,
  "nodes_read_weight" : [ {
    "id" : "45021bf73a244312a3f2af95092feeecno07",
    "weight" : 50
  }, {
    "id" : "d78a65690cea4af5ad14585e110ff89bno07",
    "weight" : 400
  } ],
  "subnet_id" : "6991d82d-9166-4aff-a1fa-d2a8748b7084"
}

响应示例

状态码: 201

Success.

{
  "job_id" : "04efe8e2-9255-44ae-a98b-d87cae411890"
}

SDK代码示例

SDK代码示例如下。

Java

开启数据库代理,规格2U8GB,代理模式只读模式。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.gaussdb.v3.region.GaussDBRegion;
import com.huaweicloud.sdk.gaussdb.v3.*;
import com.huaweicloud.sdk.gaussdb.v3.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateGaussMySqlProxySolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        GaussDBClient client = GaussDBClient.newBuilder()
                .withCredential(auth)
                .withRegion(GaussDBRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateGaussMySqlProxyRequest request = new CreateGaussMySqlProxyRequest();
        request.withInstanceId("{instance_id}");
        OpenMysqlProxyRequestBody body = new OpenMysqlProxyRequestBody();
        List<NodesWeight> listbodyNodesReadWeight = new ArrayList<>();
        listbodyNodesReadWeight.add(
            new NodesWeight()
                .withId("45021bf73a244312a3f2af95092feeecno07")
                .withWeight(50)
        );
        listbodyNodesReadWeight.add(
            new NodesWeight()
                .withId("d78a65690cea4af5ad14585e110ff89bno07")
                .withWeight(400)
        );
        body.withSubnetId("6991d82d-9166-4aff-a1fa-d2a8748b7084");
        body.withNodesReadWeight(listbodyNodesReadWeight);
        body.withRouteMode(0);
        body.withProxyMode(OpenMysqlProxyRequestBody.ProxyModeEnum.fromValue("readonly"));
        body.withProxyName("gaussdb-proxy");
        body.withNodeNum(2);
        body.withFlavorRef("gaussdb.proxy.large.x86.2");
        request.withBody(body);
        try {
            CreateGaussMySqlProxyResponse response = client.createGaussMySqlProxy(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

Python

开启数据库代理,规格2U8GB,代理模式只读模式。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkgaussdb.v3.region.gaussdb_region import GaussDBRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkgaussdb.v3 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = GaussDBClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(GaussDBRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateGaussMySqlProxyRequest()
        request.instance_id = "{instance_id}"
        listNodesReadWeightbody = [
            NodesWeight(
                id="45021bf73a244312a3f2af95092feeecno07",
                weight=50
            ),
            NodesWeight(
                id="d78a65690cea4af5ad14585e110ff89bno07",
                weight=400
            )
        ]
        request.body = OpenMysqlProxyRequestBody(
            subnet_id="6991d82d-9166-4aff-a1fa-d2a8748b7084",
            nodes_read_weight=listNodesReadWeightbody,
            route_mode=0,
            proxy_mode="readonly",
            proxy_name="gaussdb-proxy",
            node_num=2,
            flavor_ref="gaussdb.proxy.large.x86.2"
        )
        response = client.create_gauss_my_sql_proxy(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Go

开启数据库代理,规格2U8GB,代理模式只读模式。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    gaussdb "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/gaussdb/v3"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/gaussdb/v3/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/gaussdb/v3/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        Build()

    client := gaussdb.NewGaussDBClient(
        gaussdb.GaussDBClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateGaussMySqlProxyRequest{}
	request.InstanceId = "{instance_id}"
	idNodesReadWeight:= "45021bf73a244312a3f2af95092feeecno07"
	weightNodesReadWeight:= int32(50)
	idNodesReadWeight1:= "d78a65690cea4af5ad14585e110ff89bno07"
	weightNodesReadWeight1:= int32(400)
	var listNodesReadWeightbody = []model.NodesWeight{
        {
            Id: &idNodesReadWeight,
            Weight: &weightNodesReadWeight,
        },
        {
            Id: &idNodesReadWeight1,
            Weight: &weightNodesReadWeight1,
        },
    }
	subnetIdOpenMysqlProxyRequestBody:= "6991d82d-9166-4aff-a1fa-d2a8748b7084"
	routeModeOpenMysqlProxyRequestBody:= int32(0)
	proxyModeOpenMysqlProxyRequestBody:= model.GetOpenMysqlProxyRequestBodyProxyModeEnum().READONLY
	proxyNameOpenMysqlProxyRequestBody:= "gaussdb-proxy"
	request.Body = &model.OpenMysqlProxyRequestBody{
		SubnetId: &subnetIdOpenMysqlProxyRequestBody,
		NodesReadWeight: &listNodesReadWeightbody,
		RouteMode: &routeModeOpenMysqlProxyRequestBody,
		ProxyMode: &proxyModeOpenMysqlProxyRequestBody,
		ProxyName: &proxyNameOpenMysqlProxyRequestBody,
		NodeNum: int32(2),
		FlavorRef: "gaussdb.proxy.large.x86.2",
	}
	response, err := client.CreateGaussMySqlProxy(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

201

Success.

400

Client error.

500

Server error.

错误码

请参见错误码

相关文档