数据库实例的管理
初始化
参数获取可参照准备工作中华为云SDK认证方式。
Copied!
String authUrl = "****************";
String user = "****************";
String password = "****************";
String projectId = "****************";
String userDomainId = "****************";
Config config = Config.newConfig().withLanguage("zh-cn").withSSLVerificationDisabled();
OSClient.OSClientV3 osclient = OSFactory.builderV3().withConfig(config).endpoint(authUrl)
.credentials(user, password, Identifier.byId(userDomainId))
.scopeToProject(Identifier.byId(projectId)).authenticate();
InstanceManageService instanceManageService = osclient.rds().instanceManage();
创建数据库实例
创建所需部分参数
名称 |
类型 |
描述 |
name |
string |
实例名称。用于表示实例的名称,同一租户下,同类型的实例名可重名,其中,SQL Server实例名唯一。取值范围:4~64个字符之间,必须以字母开头,区分大小写,可以包含字母、数字、中划线或者下划线,不能包含其他的特殊字符。 |
volume |
Volume object |
实例存储信息。创建只读实例时不可选(只读实例的磁盘大小默认和主实例相同),其它场景必选。 |
ha |
Ha object |
HA配置参数,创建HA实例时使用。 |
datastore |
Datastore object |
数据库信息。创建主实例时必选,创建只读或者恢复到新实例时不可选。 |
backupStrategy |
BackupStrategy object |
自动备份策略。 |
flavorRef |
string |
规格码。 |
availabilityZone |
string |
可用区ID。对于数据库实例类型不是单机的实例,需要分别为实例所有节点指定可用区,并用逗号隔开。取值参见地区和终端节点。 |
vpcId |
string |
虚拟私有云ID。创建只读实例时不可选(只读实例的网络属性默认和主实例相同),其它场景必选。 |
subnetId |
string |
子网ID。创建只读实例时不可选(只读实例的网络属性默认和主实例相同),其它场景必选。 |
securityGroupId |
string |
安全组ID。创建只读实例时不可选(只读实例的网络属性默认和主实例相同),其它场景必选。 |
password |
string |
数据库密码。创建只读实例时不可选,其它场景必选。取值范围:非空,由大小写字母、数字和特殊符号~!@#%^*-_=+?组成,长度8~32个字符。<br>建议您输入高强度密码,以提高安全性,防止出现密码被暴力破解等安全风险。 |
port |
string |
数据库端口信息。1.MySQL数据库端口设置范围为1024~65535(其中12017和33071被RDS系统占用不可设置)。2.PostgreSQL数据库端口修改范围为2100~9500。3.Microsoft SQL Server实例的端口设置范围为1433和2100~9500(其中5355和5985不可设置。对于2017EE、2017SE、2017Web版,5050、5353和5986不可设置。当不传该参数时,默认端口如下:1.MySQL默认3306。2.PostgreSQL默认5432。3.Microsoft SQL Server默认1433。 |
region |
string |
区域ID。创建主实例时必选,其它场景不可选。取值参见地区和终端节点。 |
代码展示
Copied!
public static void CreatInstanceTest(InstanceManageService instanceManageService) {
DataStore datastore = DataStore.builder().type("MySQL").version("5.6").build();
Ha createha = Ha.builder().mode("Ha").replicationMode("semisync").build();
Volume createVolume = Volume.builder().type("ULTRAHIGH").size(100).build();
BackupStrategy createBackupStrategy = BackupStrategy.builder().keepDays(7)
.startTime("06:15-07:15").build();
CreateInstanceRequest CreateReq = CreateInstanceRequest.builder()
.name("JAVA100_2_S-2").volume(createVolume)
.ha(createha).datastore(datastore).backupStrategy(createBackupStrategy)
.flavorRef("rds.mysql.s1.medium.ha")
.availabilityZone("cn-north-4b,cn-north-4b")
.vpcId("3138ce3d-8837-****-b68a-4cdbc5b30a45")
.subnetId("0f48e1d1-c244-****-baa0-acfb1133c148")
.securityGroupId("702e9e18-34a2-****-a847-59546c3f5fa5")
.password("******").port("3306")
.region("cn-north-4").build();
try {
if (instanceManageService == null) {
System.out.println("[Error information]Get authenticate is null!");
return;
}
CreateInstanceResponse repCreate = instanceManageService.create(CreateReq);
System.out.println(repCreate.getInstance());
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
说明
:具体参数信息请参考API Explorer。
查询数据库实例
根据实例ID查询数据库实例
Copied!
public static void ListInstanceTest(InstanceManageService instanceManageService, String instanceId) {
try {
Map<String, String> filteringParams = new HashMap<String, String>();
filteringParams.put("offset", "0");
filteringParams.put("limit", "10");
filteringParams.put("id", instanceId);
if (instanceManageService == null) {
System.out.println("[Error information]Get authenticate is null!");
return;
}
ListInstanceResponse ListInstances = instanceManageService.list(filteringParams);
if (ListInstances != null) {
System.out.println("server list: " + ListInstances);
if (ListInstances.getInstances().size() > 0) {
for (ListInstanceResp InstanceInfor : ListInstances.getInstances()) {
System.out.println("Instances number is : " + ListInstances.getInstances().size());
System.out.println("InstanceInfor.getName: " + InstanceInfor.getName());
System.out.println("InstanceInfor.getId: " + InstanceInfor.getId());
}
}
}
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
说明
:其他条件查询,如实例名称、实例类型等请参考API Explorer。
删除数据库实例
根据实例ID删除数据库实例
Copied!
public static void DeleteInstanceTest(InstanceManageService instanceManageService, String instanceId) {
try {
if (instanceManageService == null) {
System.out.println("[Error information]Get authenticate is null!");
return;
}
ActionResponse response = instanceManageService.delete(instanceId);
if (response.getCode() != 0) {
System.out.println(response.getCode());
}
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
说明
:
重启数据库实例
Copied!
public static void RestartInstanceTest(InstanceManageService instanceManageService, String instanceId) {
try {
RestartInstanceRequest restart = RestartInstanceRequest.builder().restart("").build();
InstanceCommonResponse rep = instanceManageService.restart(restart, instanceId);
if (rep.getJobId() != "") {
System.out.println(rep.getJobId());
}
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
说明
:
- 1、具体参数说明请参考API Explorer。
- 2、实例在创建、扩容、变更规格、备份、恢复、修改端口或冻结状态下不能重启。
- 3、RDS实例重启过程中将不可用,请谨慎操作。
单机转主备实例
单机实例转主备实例
Copied!
public static void SingletohaTest(InstanceManageService instanceManageService, String instanceId) {
try {
SingleToHa single = SingleToHa.builder().azCodeNewNode("cn-north-4c").password("******").build();
SingleToHaRdsRequest singletoha = SingleToHaRdsRequest.builder().singleToHa(single).build();
if (instanceManageService == null) {
System.out.println("[Error information]Get authenticate is null!");
return;
}
InstanceCommonResponse rep = instanceManageService.singleToHa(singletoha, instanceId);
if (rep.getJobId() != "") {
System.out.println(rep.getJobId());
}
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
说明
:
- 1、具体参数说明请参考API Explorer。
- 2、包周期实例不能转为主备实例。
- 3、有些实例规格不支持升级为主备实例,具体请以实际环境为准。
变更数据库实例的规格
变更数据库实例规格
Copied!
public static void ResizeFlavorTest(InstanceManageService instanceManageService, String instanceId) {
try {
ResizeFlavor resizeFlavor = ResizeFlavor.builder().specCode("rds.mysql.m1.4xlarge.ha").build();
ResizeFlavorRequest resizeFlavorRequest = ResizeFlavorRequest.builder().resizeFlavor(resizeFlavor).build();
if (instanceManageService == null) {
System.out.println("[Error information]Get authenticate is null!");
return;
}
InstanceCommonResponse rep = instanceManageService.resizeFlavor(resizeFlavorRequest, instanceId);
if (rep.getJobId() != "") {
System.out.println(rep.getJobId());
}
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
说明
:
- 1、具体参数说明请参考API Explorer。
- 2、需要变更的数据库实例规格不能与原数据库实例规格一样。
- 3、实例状态仅为服务中时可以进行调整CPU/内存。
- 4、若属于包周期数据库实例,仅MySQL引擎支持变更规格。
- 5、只能变更相同类型数据库实例的规格。(例如,单实例只能变更为单实例对应的规格,不能变更为HA的规格)。
- 6、变更数据库实例规格时,RDS会有5~10分钟的业务中断重启,请谨慎操作。
扩容数据库实例的磁盘空间
扩容数据库实例的磁盘空间
Copied!
public static void EnlargeVolumeTest(InstanceManageService instanceManageService, String instanceId) {
try {
EnlargeVolumeSize volumeSize = EnlargeVolumeSize.builder().size(200).build();
EnlargeVolumeRequest enlargeVolumeRequest = EnlargeVolumeRequest.builder().enlargeVolume(volumeSize).build();
if (instanceManageService == null) {
System.out.println("[Error information]Get authenticate is null!");
return;
}
InstanceCommonResponse rep = instanceManageService.enlargeVolume(enlargeVolumeRequest, instanceId);
if (rep.getJobId() != "") {
System.out.println(rep.getJobId());
}
} catch (ServerResponseException e) {
System.out.println(e.getStatusCode());
}
}
说明
:
- 1、参数说明请参考API Explorer。
- 2、备实例大小和主实例大小一致,当主实例扩容时,会同时扩容备实例。
- 3、实例状态为“服务中”时可以进行扩容。
- 4、若属于包周期数据库实例,仅MySQL引擎支持扩容磁盘空间。
版本说明
本示例基于华为云SDK V3.0版本开发。
功能介绍
数据库实例是在云中运行的独立数据库环境,它是华为云关系型数据库服务的基本构建块。一个数据库实例可以包含多个由用户创建的数据库,并且可以使用与访问独立数据库实例相同的工具和应用程序进行访问。本示例为您介绍了数据库实例的创建、删除、查询以及规格变更、单一转主备等操作。
准备工作
数据库实例的管理
初始化
参数获取可参照准备工作中华为云SDK认证方式。
// endpoint url String authUrl = "****************"; String user = "****************"; String password = "****************"; String projectId = "****************"; String userDomainId = "****************"; Config config = Config.newConfig().withLanguage("zh-cn").withSSLVerificationDisabled(); OSClient.OSClientV3 osclient = OSFactory.builderV3().withConfig(config).endpoint(authUrl) .credentials(user, password, Identifier.byId(userDomainId)) .scopeToProject(Identifier.byId(projectId)).authenticate(); InstanceManageService instanceManageService = osclient.rds().instanceManage();
创建数据库实例
创建所需部分参数
代码展示
public static void CreatInstanceTest(InstanceManageService instanceManageService) { DataStore datastore = DataStore.builder().type("MySQL").version("5.6").build(); Ha createha = Ha.builder().mode("Ha").replicationMode("semisync").build(); Volume createVolume = Volume.builder().type("ULTRAHIGH").size(100).build(); BackupStrategy createBackupStrategy = BackupStrategy.builder().keepDays(7) .startTime("06:15-07:15").build(); CreateInstanceRequest CreateReq = CreateInstanceRequest.builder() .name("JAVA100_2_S-2").volume(createVolume) .ha(createha).datastore(datastore).backupStrategy(createBackupStrategy) .flavorRef("rds.mysql.s1.medium.ha") .availabilityZone("cn-north-4b,cn-north-4b") .vpcId("3138ce3d-8837-****-b68a-4cdbc5b30a45") .subnetId("0f48e1d1-c244-****-baa0-acfb1133c148") .securityGroupId("702e9e18-34a2-****-a847-59546c3f5fa5") .password("******").port("3306") .region("cn-north-4").build(); try { if (instanceManageService == null) { System.out.println("[Error information]Get authenticate is null!"); return; } CreateInstanceResponse repCreate = instanceManageService.create(CreateReq); System.out.println(repCreate.getInstance()); } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
说明
:具体参数信息请参考API Explorer。查询数据库实例
根据实例ID查询数据库实例
public static void ListInstanceTest(InstanceManageService instanceManageService, String instanceId) { try { Map<String, String> filteringParams = new HashMap<String, String>(); filteringParams.put("offset", "0"); filteringParams.put("limit", "10"); filteringParams.put("id", instanceId); if (instanceManageService == null) { System.out.println("[Error information]Get authenticate is null!"); return; } ListInstanceResponse ListInstances = instanceManageService.list(filteringParams); if (ListInstances != null) { System.out.println("server list: " + ListInstances); if (ListInstances.getInstances().size() > 0) { for (ListInstanceResp InstanceInfor : ListInstances.getInstances()) { System.out.println("Instances number is : " + ListInstances.getInstances().size()); System.out.println("InstanceInfor.getName: " + InstanceInfor.getName()); System.out.println("InstanceInfor.getId: " + InstanceInfor.getId()); } } } } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
说明
:其他条件查询,如实例名称、实例类型等请参考API Explorer。删除数据库实例
根据实例ID删除数据库实例
public static void DeleteInstanceTest(InstanceManageService instanceManageService, String instanceId) { try { if (instanceManageService == null) { System.out.println("[Error information]Get authenticate is null!"); return; } ActionResponse response = instanceManageService.delete(instanceId); if (response.getCode() != 0) { System.out.println(response.getCode()); } } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
说明
:重启数据库实例
public static void RestartInstanceTest(InstanceManageService instanceManageService, String instanceId) { try { RestartInstanceRequest restart = RestartInstanceRequest.builder().restart("").build(); InstanceCommonResponse rep = instanceManageService.restart(restart, instanceId); if (rep.getJobId() != "") { System.out.println(rep.getJobId()); } } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
说明
:单机转主备实例
单机实例转主备实例
public static void SingletohaTest(InstanceManageService instanceManageService, String instanceId) { try { SingleToHa single = SingleToHa.builder().azCodeNewNode("cn-north-4c").password("******").build(); SingleToHaRdsRequest singletoha = SingleToHaRdsRequest.builder().singleToHa(single).build(); if (instanceManageService == null) { System.out.println("[Error information]Get authenticate is null!"); return; } InstanceCommonResponse rep = instanceManageService.singleToHa(singletoha, instanceId); if (rep.getJobId() != "") { System.out.println(rep.getJobId()); } } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
说明
:变更数据库实例的规格
变更数据库实例规格
public static void ResizeFlavorTest(InstanceManageService instanceManageService, String instanceId) { try { ResizeFlavor resizeFlavor = ResizeFlavor.builder().specCode("rds.mysql.m1.4xlarge.ha").build(); ResizeFlavorRequest resizeFlavorRequest = ResizeFlavorRequest.builder().resizeFlavor(resizeFlavor).build(); if (instanceManageService == null) { System.out.println("[Error information]Get authenticate is null!"); return; } InstanceCommonResponse rep = instanceManageService.resizeFlavor(resizeFlavorRequest, instanceId); if (rep.getJobId() != "") { System.out.println(rep.getJobId()); } } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
说明
:扩容数据库实例的磁盘空间
扩容数据库实例的磁盘空间
public static void EnlargeVolumeTest(InstanceManageService instanceManageService, String instanceId) { try { EnlargeVolumeSize volumeSize = EnlargeVolumeSize.builder().size(200).build(); EnlargeVolumeRequest enlargeVolumeRequest = EnlargeVolumeRequest.builder().enlargeVolume(volumeSize).build(); if (instanceManageService == null) { System.out.println("[Error information]Get authenticate is null!"); return; } InstanceCommonResponse rep = instanceManageService.enlargeVolume(enlargeVolumeRequest, instanceId); if (rep.getJobId() != "") { System.out.println(rep.getJobId()); } } catch (ServerResponseException e) { System.out.println(e.getStatusCode()); } }
说明
:参考
更多信息请参考云数据库 RDS。