百度智能云

All Product Document

          Relational Database Service

          Primary Interfaces

          RDS Management

          The cloud database RDS (Relational Database Service) is a professional cloud database service of high-performance and highly reliability. RDS provides Web interface to configure and operate database instances, and provide you with reliable data backup and recovery, complete security management, sound monitoring, easy expansion and other functional support. Compared with self-built database, RDS is more economical, more professional, more efficient, more reliable and easy to use, which enables you to focus more on the core business.

          Create RDS master instance

          Use the following codes to create an RDS master instance

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.CreateRdsArgs{
          	// specify database engine of rds, take mysql, sqlserver, postgresql, required
              Engine:            "mysql",
              // specify database version of rds, required
              EngineVersion:  "5.6",
            // specify database version of rds, required,Postpaid;Reservation: there is no need to set it when the payment method is postpaid but it must be set when the payment method is prepaid; required
              Billing: rds.Billing{
                  PaymentTiming: "Postpaid",
                  //Reservation: rds.Reservation{ReservationLength: 1, ReservationTimeUnit: "Month"},
              },
              // CPU count, required
              CpuCount: 1,
              //package memory capacity, in GB, required
              MemoryCapacity: 1,
              //package disk capacity, in GB, increasing every 5G
              VolumeCapacity: 5,
              //number of RDS instances created in batch, not more than 10, 1 by default, optional
              PurchaseCount: 1,
              //rds instance name, lowercase letters and numbers allowed, length limited to 1~32, default naming rule:{engine} + {engineVersion}, optional
              InstanceName: "instanceName",
              //series, Basic:stand-alone basic version, Standard:dual-engine HA version. Only SQLServer 2012sp3 supports stand-alone basic version. Standard by default, optional
              Category: "Standard",
              //specify zone information, blank by default, selected by system automatically, optional
              //zoneName can be named in this way "country -region- available region sequence" in lowercase, for example, Beijing available region A is "cn-bj-a".
              ZoneNames: ["cn-bj-a"],
              //vpc, if not provided, then take the default vpc, optional
              VpcId: "vpc-IyrqYIQ7",
              //whether to pay directly, false by default, deduction will be made directly for the change order set as pay directly, and there is no need to perform the payment logic, optional
              IsDirectPay: false,
              /subnetId of each available area in vpc; If it is not the default vpc, the subnetId must be specified, optional
              Subnets: []rds.SubnetMap{
                  {
                      ZoneName: "cn-bj-a",
                      SubnetId: "sbn-IyWRnII7",
                  },   
              },
              // label information bound to instance, optional
              Tags: []model.TagModel{
                  {
                      TagKey:   "tagK",
                      TagValue: "tagV",
                  },
              },
          }
          result, err := client.CreateRds(args)
          if err != nil {
              fmt.Printf("create rds error: %+v\n", err)
              return
          }
          
          for _, e := range result.InstanceIds {
          	fmt.Println("create rds success, instanceId: ", e)
          }

          Note:

          • For instance packages available, see details in (https://cloud.baidu.com/doc/RDS/s/9jwvz0wd3)
          • The billing method for postpaid instance is, the account cash balance+general vouchers should be greater than 100; and the billing method for prepaid instance is, the account cash balance should be greater than or equal to the instance fee.
          • batch creation is supported, and if the creation of any one of instances fails, all instances will be rolled back and the creation of all of them will fail.
          • The creation of interface is an asynchronous operation, the status of instance can be queried by the interface for querying specified instance details.

          Create RDS read-only instance

          Use the following codes to create an RDS read-only instance

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.CreateReadReplicaArgs{
              //master instance ID, required
              SourceInstanceId: "sourceInstanceId"
              // billing related parameters, read-only instances only support Postpaid, required
              Billing: rds.Billing{
                  PaymentTiming: "Postpaid",
              },
              // CPU count, required
              CpuCount: 1,
              //package memory capacity, in GB, required
              MemoryCapacity: 1,
              //package disk capacity, in GB, increasing every 5G, required
              VolumeCapacity: 5,
              //batch creation of RDS read-only instances of cloud database. Currently only one instance is supported, optional
              PurchaseCount: 1,
              //Instance name, lowercase letters and numbers allowed, length limited to 1~32, etc. default naming rule:{engine} + {engineVersion}, optional
              InstanceName: "instanceName",
              //specify zone information, blank by default, selected by system automatically, optional
              //the zoneName is named by the following way "country -region- available region sequence" in lowercase, for example, Beijing available region A is "cn-bj-a".
              ZoneNames: ["cn-bj-a"],
              //same as the vpcId of master instance, optional
              VpcId: "vpc-IyrqYIQ7",
              //whether to pay directly, false by default, deduction will be made directly for the change order set as pay directly, and there is no need to perform the payment logic, optional
              IsDirectPay: false,
              //subnetId of each available area in vpc; if it is not the default vpc, the subnetId must be specified, optional
              Subnets: []rds.SubnetMap{
                  {
                      ZoneName: "cn-bj-a",
                      SubnetId: "sbn-IyWRnII7",
                  },   
              },
              // label information bound to instance, optional
              Tags: []model.TagModel{
                  {
                      TagKey:   "tagK",
                      TagValue: "tagV",
                  },
              },
          }
          result, err := client.CreateReadReplica(args)
          if err != nil {
              fmt.Printf("create rds readReplica error: %+v\n", err)
              return
          }
          
          for _, e := range result.InstanceIds {
          	fmt.Println("create rds readReplica success, instanceId: ", e)
          }

          Note:

          • It is required to create on the basis of the RDS master instance
          • For instance packages available, see details in (https://cloud.baidu.com/doc/RDS/s/9jwvz0wd3)
          • Only the master instance of which the database type is MySQL supports the creation of read-only instances
          • The database engine and database version of the read-only instance are the same as those of the master instance, which has no need to set up, the minimum version of the master instance is MySQL 5.6
          • The disk capacity of the read-only instance cannot be less than that of the master instance
          • The vpcId of the read-only instance should be consistent with the master instance
          • An RDS instance can only have 5 read-only instances at most, and only one instance can be created at a time
          • The read-only instances can be purchased by post-paid method only

          Create RDS proxy instance

          Use the following codes to create an RDS proxy instance

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.CreateRdsProxyArgs{
              //master instance ID, required
              SourceInstanceId: "sourceInstanceId"
              // billing related parameters, the proxy instance only supports Postpaid, required
              Billing: rds.Billing{
                  PaymentTiming: "Postpaid",
              },
              // number of agent instance nodes. The value range is 2, 4, 6, 8, 16, required
              NodeAmount: 2,
              //instance name, lowercase letters and numbers are allowed, the length is limited to 1~32, and the default naming rule is:{engine} + {engineVersion},optional
              InstanceName: "instanceName",
              //specify zone information, blank by default, automatically selected by the system. optional
              //zoneName can be named in this way "country -region- available region sequence" in lowercase, for example, Beijing available area A is" cn-bj-a ", so it is suggested to keep the available region same as that of the master instance
              ZoneNames: ["cn-bj-a"],
              //same as master instance vpcID, optional
              VpcId: "vpc-IyrqYIQ7",
              //whether to pay directly, false by default, deduction will be made directly for the change order set as pay directly, and there is no need to perform the payment logic, optional
              IsDirectPay: false,
              ///subnetId of each available area in vpc; if it is not the default vpc, the subnetId must be specified, optional
              Subnets: []rds.SubnetMap{
                  {
                      ZoneName: "cn-bj-a",
                      SubnetId: "sbn-IyWRnII7",
                  },   
              },
              // label information bound to instance, optional
              Tags: []model.TagModel{
                  {
                      TagKey:   "tagK",
                      TagValue: "tagV",
                  },
              },
          }
          result, err := client.CreateRdsProxy(args)
          if err != nil {
              fmt.Printf("create rds proxy error: %+v\n", err)
              return
          }
          
          for _, e := range result.InstanceIds {
          	fmt.Println("create rds proxy success, instanceId: ", e)
          }

          Note:

          • It is required to create on the basis of the RDS master instance
          • Only the master instance of which the database type is MySQL supports the creation of read-only instances
          • The proxy instance package is bound to the master instance package, and the minimum version of the master instance is MySQL 5.6
          • At most one proxy instance can be created under one master instance
          • The proxy instance should be created in the same vpc as the master instance
          • The proxy instance should be created in the same vpc as the master instance

          Query RDS List

          Use the following codes to query the RDS list.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.ListRdsArgs{
              // get the starting position of the list query in batch, which is a string generated by the system, optional
              Marker: "marker",
              // specify the maximum number (master instance) contained in each page, the maximum number does not exceed 1000, and the default value is 1000, optional
              MaxKeys: 1,
          }
          result, err := client.ListRds(args)
          if err != nil {
              fmt.Printf("list rds error: %+v\n", err)
              return
          }
          
          // return the starting position of the marked query
          fmt.Println("rds list marker: ", result.Marker)
          // true indicates that there are data behind, and false indicates that it is the last page
          fmt.Println("rds list isTruncated: ", result.IsTruncated)
          // get the marker value to be transmitted on the next page. When isTruncated is false, the domain does not appear
          fmt.Println("rds list nextMarker: ", result.NextMarker)
          // maximum number contained per page
          fmt.Println("rds list maxKeys: ", result.MaxKeys)
          // get the list information of rds
          for _, e := range result.Instances {
              fmt.Println("rds instanceId: ", e.InstanceId)
              fmt.Println("rds instanceName: ", e.InstanceName)
              fmt.Println("rds engine: ", e.Engine)
              fmt.Println("rds engineVersion: ", e.EngineVersion)
              fmt.Println("rds instanceStatus: ", e.InstanceStatus)
              fmt.Println("rds cpuCount: ", e.CpuCount)
              fmt.Println("rds memoryCapacity: ", e.MemoryCapacity)
              fmt.Println("rds volumeCapacity: ", e.VolumeCapacity)
              fmt.Println("rds usedStorage: ", e.UsedStorage)
              fmt.Println("rds paymentTiming: ", e.PaymentTiming)
              fmt.Println("rds instanceType: ", e.InstanceType)
              fmt.Println("rds instanceCreateTime: ", e.InstanceCreateTime)
              fmt.Println("rds instanceExpireTime: ", e.InstanceExpireTime)
              fmt.Println("rds publicAccessStatus: ", e.PublicAccessStatus)
              fmt.Println("rds vpcId: ", e.VpcId)
          }

          Note:

          • Only the list of instances under own account can be viewed.
          • The interface divides each master instance and its read-only and proxy instances under the master instance into a group, parameter maxKeys represents the number of groups, that is, the number of master instances.

          Query the specified RDS instance information

          Use the following codes to query the specified RDS instance information.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          result, err := client.GetDetail(instanceId)
          if err != nil {
              fmt.Printf("get rds detail error: %+v\n", err)
              return
          }
          
          fmt.Println("rds instanceId: ", result.InstanceId)
          fmt.Println("rds instanceName: ", result.InstanceName)
          fmt.Println("rds engine: ", result.Engine)
          fmt.Println("rds engineVersion: ", result.EngineVersion)
          fmt.Println("rds instanceStatus: ", result.InstanceStatus)
          fmt.Println("rds cpuCount: ", result.CpuCount)
          fmt.Println("rds memoryCapacity: ", result.MemoryCapacity)
          fmt.Println("rds volumeCapacity: ", result.VolumeCapacity)
          fmt.Println("rds usedStorage: ", result.UsedStorage)
          fmt.Println("rds paymentTiming: ", result.PaymentTiming)
          fmt.Println("rds instanceType: ", result.InstanceType)
          fmt.Println("rds instanceCreateTime: ", result.InstanceCreateTime)
          fmt.Println("rds instanceExpireTime: ", result.InstanceExpireTime)
          fmt.Println("rds publicAccessStatus: ", result.PublicAccessStatus)
          fmt.Println("rds vpcId: ", result.VpcId)

          Delete RDS instance

          Use the following codes to delete the RDS list.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          //multiple instances are separated by English comma ",", and up to 10 can be entered.
          if err := client.DeleteRds(instanceIds); err != nil {
              fmt.Printf("delete rds error: %+v\n", err)
              return
          }
          fmt.Printf("delete rds success\n")

          Note:

          • Only the Postpaid instances or the expired instances of which the payment type of Prepaid can be released.
          • If the master instance is released, then the read-only instances and proxy instances associated with the master instance will also be released.

          RDS instance capacity expansion or reduction

          Use the following codes to expand and reduce the RDS instance.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.ResizeRdsArgs{
          	// cpu count
              CpuCount: 2,
              // memory capacity, in GB
              MemoryCapacity: 8,
              // disk capacity, in GB, increasing every 5G
              VolumeCapacity: 20,
              // number of proxy instance nodes, required when proxy instance is changed
              NodeAmount: 2,
              // whether to pay directly, false by default, deduction will be made directly for the change order set as pay directly, and there is no need to perform the payment logic, optional
              IsDirectPay: false,
          }
          err = client.ResizeRds(instanceId, args)
          if err != nil {
              fmt.Printf("resize rds error: %+v\n", err)
              return
          }
          
          fmt.Println("resize rds success.")

          Note:

          • For instance packages available, see details in (https://cloud.baidu.com/doc/RDS/s/9jwvz0wd3)
          • At least one of cpuCount, memoryCapacity and volumeCapacity should be filled in when the master instancee or read-only instance is changed.
          • When the billing method of instance is post-paid, the capacity can be expanded or reduced flexibly; when the billing method is prepaid, the capacity cannot be reduced.
          • The capacity expansion or reduction can be performed only when the instance is in the available status.
          • The instance will be restarted once after the capacity is expanded or reduced.
          • It is an asynchronous interface. Whether instanceStatus is restored can be viewed by the interface for querying the instance details.

          Restart instance

          Use the following codes to restart the instance.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          err := client.RebootInstance(instanceId)
          if err != nil {
              fmt.Printf("reboot rds error: %+v\n", err)
              return
          }

          Modify instance name

          Use the following codes to modify the RDS instance name.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.UpdateInstanceNameArgs{
              InstanceName: "instanceName",
          }
          err = client.UpdateInstanceName(instanceId, args)
          if err != nil {
              fmt.Printf("update instance name error: %+v\n", err)
              return
          }
          fmt.Printf("update instance name success\n")

          Note:

          • The instance names can contain uppercase and lowercase letters, numbers and special characters such as-_/. but it must begin with letters and have a length of 1-64.

          Modify synchronization mode

          Use the following codes to modify the RDS instance synchronization mode.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.ModifySyncModeArgs{
              //"Async"asynchronous replication,"Semi_sync" semi-synchronous replication
              SyncMode: "Async",
          }
          err = client.ModifySyncMode(instanceId, args)
          if err != nil {
              fmt.Printf("modify syncMode error: %+v\n", err)
              return
          }
          fmt.Printf("modify syncMode success\n")

          Modify connection information

          Use the following codes to modify the RDS domain name prefix.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.ModifyEndpointArgs{
              Address: "newAddress",
          }
          err = client.ModifyEndpoint(instanceId, args)
          if err != nil {
              fmt.Printf("modify endpoint error: %+v\n", err)
              return
          }
          fmt.Printf("modify endpoint success\n")

          Note:

          • It only needs to transmit the prefix of the domain name. The prefix of domain name consists of lowercase letters and numbers, beginning with lowercase letters, between 3 and 30 digits.

          Open and close public network access

          Use the following codes to modify the RDS domain name prefix.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.ModifyPublicAccessArgs{
              // true or false
              PublicAccess: true,
          }
          err = client.ModifyPublicAccess(instanceId, args)
          if err != nil {
              fmt.Printf("modify public access error: %+v\n", err)
              return
          }
          fmt.Printf("modify public access success\n")

          Note:

          • true:Open public network access; false:Close public network access.

          Account Management

          Create an account

          Use the following codes to create a new account under a master instance.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          args := &rds.CreateAccountArgs{
          	// account name, which cannot be a reserved keyword, required
              AccountName: "accountName",
              // The password of account, composed of letters, numbers or underscores, 6 ~ 32 digits,the password should be transmitted after being encrypted, and is not allowed to be transmitted in plaintext, required
              Password: "password",
              // account privilege type,Common:common account,Super:super account. Common account by default, optional
              AccountType: "Common",
              // this item can be set under MySQL and SQL Server instances, optional
              DatabasePrivileges: []rds.DatabasePrivilege{
                      {
                          //database name
                          DbName: "user_photo_001",
                          //authorization type. ReadOnly:read only, ReadWrite:read and write
                          AuthType: "ReadOnly",
                      },   
                  },
              // account description information, optional
              Desc: "account user1", 
              // account attribution type, OnlyMaster:account used on master instance, RdsProxy:account used on proxy instance corresponding to the master instance. OnlyMaster account by default, optional
              Type: "OnlyMaster",
          }
          err = client.CreateAccount(instanceId, args)
          if err != nil {
              fmt.Printf("create account error: %+v\n", err)
              return
          }
          
          fmt.Println("create account success.")

          Note:

          • The status of instance is Available, and the instance must be a master instance.
          • Not exceed the maximum number of accounts under an instance.
          • If the database engine of the instance is PostgreSQL, only Super account can be created. The operations for other accounts and database are managed through this Super account.
          • If the database engine of the instance is MySQL, any type of account can be created.
          • If the database engine of the instance is SQLServer, only Common accounts can be created.

          Query account list

          Use the following codes to query the account list under specified instance.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          result, err := client.ListAccount(instanceId)
          if err != nil {
              fmt.Printf("list account error: %+v\n", err)
              return
          }
          
          // get account list information
          for _, e := range result.Accounts {
              fmt.Println("rds accountName: ", e.AccountName)
              fmt.Println("rds desc: ", e.Desc)
              fmt.Println("rds status: ", e.Status)
              fmt.Println("rds type: ", e.Type)
              fmt.Println("rds accountType: ", e.AccountType)
          }

          Query specific account information

          Use the following codes to query specific account information.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          result, err := client.GetAccount(instanceId,accountName)
          if err != nil {
              fmt.Printf("get account error: %+v\n", err)
              return
          }
          
          // get account list information
          fmt.Println("rds accountName: ", result.AccountName)
          fmt.Println("rds desc: ", result.Desc)
          fmt.Println("rds status: ", result.Status)
          fmt.Println("rds type: ", result.Type)
          fmt.Println("rds accountType: ", result.AccountType)

          Delete specific account information

          Use the following codes to delete specific account information.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          
          result, err := client.DeleteAccount(instanceId,accountName)
          if err != nil {
              fmt.Printf("delete account error: %+v\n", err)
              return
          }
          fmt.Printf("delete account success\n")

          Others

          Get backup list

          Use the following codes to get the backup list under an instance.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          args := &rds.GetBackupListArgs{}
          _, err := client.GetBackupList(instanceId, args)
          if err != nil {
              fmt.Printf("get backup list error: %+v\n", err)
              return
          }
          fmt.Printf("get backup list success\n")

          Note:

          • The request parameters marker and maxKeys are not required.

          Get the list of available regions

          Use the following codes to get the list of available regions.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          err = client.GetZoneList()
          if err != nil {
              fmt.Printf("get zone list error: %+v\n", err)
              return
          }
          fmt.Printf("get zone list success\n")
          fmt.Println("rds instanceId: ", result.InstanceId)

          Get subnet list

          Use the following codes to get the subnet list under an instance.

          // import "github.com/baidubce/bce-sdk-go/services/rds"
          args := &rds.ListSubnetsArgs{}
          _, err := client.ListSubnets(args)
          if err != nil {
              fmt.Printf("get subnet list error: %+v\n", err)
              return
          }
          fmt.Printf("get subnet list success\n")

          Note:

          • The request parameters vpcId and zoneName are not required.
          Previous
          Exceptions Handling
          Next
          Other Interfaces