使用 Amazon SDK 创建 Aurora 数据库集群参数组 - Amazon Aurora
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Amazon SDK 创建 Aurora 数据库集群参数组

以下代码示例显示了如何创建 Aurora 数据库集群参数组。

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

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

/// <summary> /// Create a custom cluster parameter group. /// </summary> /// <param name="parameterGroupFamily">The family of the parameter group.</param> /// <param name="groupName">The name for the new parameter group.</param> /// <param name="description">A description for the new parameter group.</param> /// <returns>The new parameter group object.</returns> public async Task<DBClusterParameterGroup> CreateCustomClusterParameterGroupAsync( string parameterGroupFamily, string groupName, string description) { var request = new CreateDBClusterParameterGroupRequest { DBParameterGroupFamily = parameterGroupFamily, DBClusterParameterGroupName = groupName, Description = description, }; var response = await _amazonRDS.CreateDBClusterParameterGroupAsync(request); return response.DBClusterParameterGroup; }
C++
适用于 C++ 的 SDK
注意

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

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); Aws::RDS::Model::CreateDBClusterParameterGroupRequest request; request.SetDBClusterParameterGroupName(CLUSTER_PARAMETER_GROUP_NAME); request.SetDBParameterGroupFamily(dbParameterGroupFamily); request.SetDescription("Example cluster parameter group."); Aws::RDS::Model::CreateDBClusterParameterGroupOutcome outcome = client.CreateDBClusterParameterGroup(request); if (outcome.IsSuccess()) { std::cout << "The DB cluster parameter group was successfully created." << std::endl; } else { std::cerr << "Error with Aurora::CreateDBClusterParameterGroup. " << outcome.GetError().GetMessage() << std::endl; return false; }
Go
适用于 Go V2 的 SDK
注意

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

type DbClusters struct { AuroraClient *rds.Client } // CreateParameterGroup creates a DB cluster parameter group that is based on the specified // parameter group family. func (clusters *DbClusters) CreateParameterGroup( parameterGroupName string, parameterGroupFamily string, description string) ( *types.DBClusterParameterGroup, error) { output, err := clusters.AuroraClient.CreateDBClusterParameterGroup(context.TODO(), &rds.CreateDBClusterParameterGroupInput{ DBClusterParameterGroupName: aws.String(parameterGroupName), DBParameterGroupFamily: aws.String(parameterGroupFamily), Description: aws.String(description), }) if err != nil { log.Printf("Couldn't create parameter group %v: %v\n", parameterGroupName, err) return nil, err } else { return output.DBClusterParameterGroup, err } }
Java
SDK for Java 2.x
注意

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

public static void createDBClusterParameterGroup(RdsClient rdsClient, String dbClusterGroupName, String dbParameterGroupFamily) { try { CreateDbClusterParameterGroupRequest groupRequest = CreateDbClusterParameterGroupRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .dbParameterGroupFamily(dbParameterGroupFamily) .description("Created by using the AWS SDK for Java") .build(); CreateDbClusterParameterGroupResponse response = rdsClient.createDBClusterParameterGroup(groupRequest); System.out.println("The group name is " + response.dbClusterParameterGroup().dbClusterParameterGroupName()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
Kotlin
适用于 Kotlin 的 SDK
注意

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

suspend fun createDBClusterParameterGroup(dbClusterGroupNameVal: String?, dbParameterGroupFamilyVal: String?) { val groupRequest = CreateDbClusterParameterGroupRequest { dbClusterParameterGroupName = dbClusterGroupNameVal dbParameterGroupFamily = dbParameterGroupFamilyVal description = "Created by using the AWS SDK for Kotlin" } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbClusterParameterGroup(groupRequest) println("The group name is ${response.dbClusterParameterGroup?.dbClusterParameterGroupName}") } }
Python
SDK for Python(Boto3)
注意

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

class AuroraWrapper: """Encapsulates Aurora DB cluster actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon Relational Database Service (Amazon RDS) client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def create_parameter_group( self, parameter_group_name, parameter_group_family, description ): """ Creates a DB cluster parameter group that is based on the specified parameter group family. :param parameter_group_name: The name of the newly created parameter group. :param parameter_group_family: The family that is used as the basis of the new parameter group. :param description: A description given to the parameter group. :return: Data about the newly created parameter group. """ try: response = self.rds_client.create_db_cluster_parameter_group( DBClusterParameterGroupName=parameter_group_name, DBParameterGroupFamily=parameter_group_family, Description=description, ) except ClientError as err: logger.error( "Couldn't create parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response
Rust
适用于 Rust 的 SDK
注意

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

// Select an engine family and create a custom DB cluster parameter group. rds.CreateDbClusterParameterGroup(DBParameterGroupFamily='aurora-mysql8.0') pub async fn set_engine(&mut self, engine: &str, version: &str) -> Result<(), ScenarioError> { self.engine_family = Some(engine.to_string()); self.engine_version = Some(version.to_string()); let create_db_cluster_parameter_group = self .rds .create_db_cluster_parameter_group( DB_CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_PARAMETER_GROUP_DESCRIPTION, engine, ) .await; match create_db_cluster_parameter_group { Ok(CreateDbClusterParameterGroupOutput { db_cluster_parameter_group: None, .. }) => { return Err(ScenarioError::with( "CreateDBClusterParameterGroup had empty response", )); } Err(error) => { if error.code() == Some("DBParameterGroupAlreadyExists") { info!("Cluster Parameter Group already exists, nothing to do"); } else { return Err(ScenarioError::new( "Could not create Cluster Parameter Group", &error, )); } } _ => { info!("Created Cluster Parameter Group"); } } Ok(()) } pub async fn create_db_cluster_parameter_group( &self, name: &str, description: &str, family: &str, ) -> Result<CreateDbClusterParameterGroupOutput, SdkError<CreateDBClusterParameterGroupError>> { self.inner .create_db_cluster_parameter_group() .db_cluster_parameter_group_name(name) .description(description) .db_parameter_group_family(family) .send() .await } #[tokio::test] async fn test_scenario_set_engine() { let mut mock_rds = MockRdsImpl::default(); mock_rds .expect_create_db_cluster_parameter_group() .with( eq("RustSDKCodeExamplesDBParameterGroup"), eq("Parameter Group created by Rust SDK Code Example"), eq("aurora-mysql"), ) .return_once(|_, _, _| { Ok(CreateDbClusterParameterGroupOutput::builder() .db_cluster_parameter_group(DbClusterParameterGroup::builder().build()) .build()) }); let mut scenario = AuroraScenario::new(mock_rds); let set_engine = scenario.set_engine("aurora-mysql", "aurora-mysql8.0").await; assert_eq!(set_engine, Ok(())); assert_eq!(Some("aurora-mysql"), scenario.engine_family.as_deref()); assert_eq!(Some("aurora-mysql8.0"), scenario.engine_version.as_deref()); } #[tokio::test] async fn test_scenario_set_engine_not_create() { let mut mock_rds = MockRdsImpl::default(); mock_rds .expect_create_db_cluster_parameter_group() .with( eq("RustSDKCodeExamplesDBParameterGroup"), eq("Parameter Group created by Rust SDK Code Example"), eq("aurora-mysql"), ) .return_once(|_, _, _| Ok(CreateDbClusterParameterGroupOutput::builder().build())); let mut scenario = AuroraScenario::new(mock_rds); let set_engine = scenario.set_engine("aurora-mysql", "aurora-mysql8.0").await; assert!(set_engine.is_err()); } #[tokio::test] async fn test_scenario_set_engine_param_group_exists() { let mut mock_rds = MockRdsImpl::default(); mock_rds .expect_create_db_cluster_parameter_group() .withf(|_, _, _| true) .return_once(|_, _, _| { Err(SdkError::service_error( CreateDBClusterParameterGroupError::DbParameterGroupAlreadyExistsFault( DbParameterGroupAlreadyExistsFault::builder().build(), ), Response::new(StatusCode::try_from(400).unwrap(), SdkBody::empty()), )) }); let mut scenario = AuroraScenario::new(mock_rds); let set_engine = scenario.set_engine("aurora-mysql", "aurora-mysql8.0").await; assert!(set_engine.is_err()); }

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