将 ModifyDBInstance 与 Amazon SDK 或 CLI 配合使用 - Amazon Relational Database Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

ModifyDBInstance 与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 ModifyDBInstance

CLI
Amazon CLI

示例 1:修改数据库实例

以下 modify-db-instance 示例将选项组和参数组与兼容的 Microsoft SQL Server 数据库实例关联起来。--apply-immediately 参数使选项和参数组立即关联,而不是等到下一个维护时段。

aws rds modify-db-instance \ --db-instance-identifier database-2 \ --option-group-name test-se-2017 \ --db-parameter-group-name test-sqlserver-se-2017 \ --apply-immediately

输出:

{ "DBInstance": { "DBInstanceIdentifier": "database-2", "DBInstanceClass": "db.r4.large", "Engine": "sqlserver-se", "DBInstanceStatus": "available", ...output omitted... "DBParameterGroups": [ { "DBParameterGroupName": "test-sqlserver-se-2017", "ParameterApplyStatus": "applying" } ], "AvailabilityZone": "us-west-2d", ...output omitted... "MultiAZ": true, "EngineVersion": "14.00.3281.6.v1", "AutoMinorVersionUpgrade": false, "ReadReplicaDBInstanceIdentifiers": [], "LicenseModel": "license-included", "OptionGroupMemberships": [ { "OptionGroupName": "test-se-2017", "Status": "pending-apply" } ], "CharacterSetName": "SQL_Latin1_General_CP1_CI_AS", "SecondaryAvailabilityZone": "us-west-2c", "PubliclyAccessible": true, "StorageType": "gp2", ...output omitted... "DeletionProtection": false, "AssociatedRoles": [], "MaxAllocatedStorage": 1000 } }

有关更多信息,请参阅《Amazon RDS 用户指南》中的修改 Amazon RDS 数据库实例

示例 2:将 VPC 安全组与数据库实例关联

以下 modify-db-instance 示例关联特定 VPC 安全组并从数据库实例中移除数据库安全组:

aws rds modify-db-instance \ --db-instance-identifier dbName \ --vpc-security-group-ids sg-ID

输出:

{ "DBInstance": { "DBInstanceIdentifier": "dbName", "DBInstanceClass": "db.t3.micro", "Engine": "mysql", "DBInstanceStatus": "available", "MasterUsername": "admin", "Endpoint": { "Address": "dbName.abcdefghijk.us-west-2.rds.amazonaws.com", "Port": 3306, "HostedZoneId": "ABCDEFGHIJK1234" }, "AllocatedStorage": 20, "InstanceCreateTime": "2024-02-15T00:37:58.793000+00:00", "PreferredBackupWindow": "11:57-12:27", "BackupRetentionPeriod": 7, "DBSecurityGroups": [], "VpcSecurityGroups": [ { "VpcSecurityGroupId": "sg-ID", "Status": "active" } ], ... output omitted ... "MultiAZ": false, "EngineVersion": "8.0.35", "AutoMinorVersionUpgrade": true, "ReadReplicaDBInstanceIdentifiers": [], "LicenseModel": "general-public-license", ... output ommited ... } }

有关更多信息,请参阅《Amazon RDS 用户指南》中的使用安全组控制访问权限

  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 ModifyDBInstance

Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rds.RdsClient; import software.amazon.awssdk.services.rds.model.ModifyDbInstanceRequest; import software.amazon.awssdk.services.rds.model.ModifyDbInstanceResponse; import software.amazon.awssdk.services.rds.model.RdsException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ModifyDBInstance { public static void main(String[] args) { final String usage = """ Usage: <dbInstanceIdentifier> <dbSnapshotIdentifier>\s Where: dbInstanceIdentifier - The database instance identifier.\s masterUserPassword - The updated password that corresponds to the master user name.\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String dbInstanceIdentifier = args[0]; String masterUserPassword = args[1]; Region region = Region.US_WEST_2; RdsClient rdsClient = RdsClient.builder() .region(region) .build(); updateIntance(rdsClient, dbInstanceIdentifier, masterUserPassword); rdsClient.close(); } public static void updateIntance(RdsClient rdsClient, String dbInstanceIdentifier, String masterUserPassword) { try { // For a demo - modify the DB instance by modifying the master password. ModifyDbInstanceRequest modifyDbInstanceRequest = ModifyDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .publiclyAccessible(true) .masterUserPassword(masterUserPassword) .build(); ModifyDbInstanceResponse instanceResponse = rdsClient.modifyDBInstance(modifyDbInstanceRequest); System.out.print("The ARN of the modified database is: " + instanceResponse.dbInstance().dbInstanceArn()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 ModifyDBInstance

Kotlin
适用于 Kotlin 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

suspend fun updateIntance( dbInstanceIdentifierVal: String?, masterUserPasswordVal: String?, ) { val request = ModifyDbInstanceRequest { dbInstanceIdentifier = dbInstanceIdentifierVal publiclyAccessible = true masterUserPassword = masterUserPasswordVal } RdsClient { region = "us-west-2" }.use { rdsClient -> val instanceResponse = rdsClient.modifyDbInstance(request) println("The ARN of the modified database is ${instanceResponse.dbInstance?.dbInstanceArn}") } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Kotlin API 参考》中的 ModifyDBInstance

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。