Deploy MySQL Single Instance
Application Scenario
Huawei Cloud Relational Database Service (RDS) MySQL single instance functionality provides highly available, high-performance MySQL database services, supporting enterprise-level functions such as automatic backup, monitoring alerts, and elastic scaling. By configuring MySQL single instances, you can quickly deploy production-grade MySQL databases, meeting database requirements for scenarios such as web applications, enterprise systems, and data analysis.
This best practice is particularly suitable for scenarios that require rapid MySQL database deployment, implementing persistent data storage, and building enterprise application backends, such as web application development, enterprise management systems, data analysis platforms, etc. This best practice will introduce how to use Terraform to automatically deploy RDS MySQL single instances, including VPC network, security group, RDS instance, database account, database, and backup creation, implementing a complete MySQL database management solution.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
Operation Steps
1. Script Preparation
Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.
2. Create VPC Network
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a VPC resource:
Parameter Description:
name: VPC name, assigned by referencing the input variable vpc_name
cidr: VPC CIDR block, assigned by referencing the input variable vpc_cidr, default value is "192.168.0.0/16"
3. Query Availability Zone Information
Add the following script to the TF file to instruct Terraform to query availability zone information:
Parameter Description:
count: Conditional creation, creates this data source when availability_zone variable is an empty string
4. Create VPC Subnet
Add the following script to the TF file to instruct Terraform to create a VPC subnet resource:
Parameter Description:
vpc_id: VPC ID that the subnet belongs to, referencing the ID of the previously created VPC resource
name: Subnet name, assigned by referencing the input variable subnet_name
cidr: Subnet CIDR block, automatically calculated if subnet_cidr is empty, otherwise uses subnet_cidr value
gateway_ip: Subnet gateway IP, automatically calculated if gateway_ip is empty, otherwise uses gateway_ip value
availability_zone: Availability zone that the subnet belongs to, prioritizes using availability_zone variable, uses the first queried availability zone if empty
5. Query RDS Flavor Information
Add the following script to the TF file to instruct Terraform to query RDS flavor information:
Parameter Description:
count: Conditional creation, creates this data source when instance_flavor_id variable is an empty string
db_type: Database engine type, assigned by referencing the input variable instance_db_type, default value is "MySQL"
db_version: Database engine version, assigned by referencing the input variable instance_db_version, default value is "8.0"
instance_mode: Instance mode, assigned by referencing the input variable instance_mode, default value is "single"
group_type: Flavor group type, assigned by referencing the input variable instance_flavor_group_type, default value is "general"
vcpus: Number of CPU cores, assigned by referencing the input variable instance_flavor_vcpus, default value is 2
availability_zone: Availability zone, prioritizes using availability_zone variable, uses the first queried availability zone if empty
6. Create Security Group
Add the following script to the TF file to instruct Terraform to create a security group resource:
Parameter Description:
name: Security group name, assigned by referencing the input variable security_group_name
delete_default_rules: Whether to delete default rules, set to true to delete default security group rules
7. Create Security Group Rules
Add the following script to the TF file to instruct Terraform to create security group rule resources:
Parameter Description:
security_group_id: Security group ID, referencing the ID of the previously created security group resource
direction: Rule direction, set to "ingress" for inbound rules
ethertype: IP protocol type, set to "IPv4" for IPv4 protocol
remote_ip_prefix: Remote IP prefix, using VPC CIDR block
ports: Port number, assigned by referencing the input variable instance_db_port, default value is 3306
protocol: Protocol type, set to "tcp" for TCP protocol
8. Create Random Password
Add the following script to the TF file to instruct Terraform to create a random password resource:
Parameter Description:
count: Conditional creation, creates this resource when instance_password variable is an empty string
length: Password length, set to 12 characters
special: Whether to include special characters, set to true to include special characters
override_special: Special character set, set to "!@%^*-_=+"
9. Create RDS Instance
Add the following script to the TF file to instruct Terraform to create an RDS instance resource:
Parameter Description:
name: RDS instance name, assigned by referencing the input variable instance_name
flavor: Instance flavor, prioritizes using instance_flavor_id variable, uses queried flavor name if empty
vpc_id: VPC ID, referencing the ID of the previously created VPC resource
subnet_id: Subnet ID, referencing the ID of the previously created VPC subnet resource
security_group_id: Security group ID, referencing the ID of the previously created security group resource
availability_zone: Availability zone list, prioritizes using availability_zone variable, uses queried availability zone if empty
db: Database configuration block
type: Database engine type, assigned by referencing the input variable instance_db_type
version: Database engine version, assigned by referencing the input variable instance_db_version
port: Database port, assigned by referencing the input variable instance_db_port
password: Database password, prioritizes using instance_password variable, uses randomly generated password if empty
volume: Storage volume configuration block
type: Storage type, assigned by referencing the input variable instance_volume_type, default value is "CLOUDSSD"
size: Storage size, assigned by referencing the input variable instance_volume_size, default value is 40 (GB)
backup_strategy: Backup strategy configuration block
start_time: Backup time window, assigned by referencing the input variable instance_backup_time_window
keep_days: Backup retention days, assigned by referencing the input variable instance_backup_keep_days
lifecycle.ignore_changes: Lifecycle management, ignores flavor changes
10. Create RDS MySQL Account
Add the following script to the TF file to instruct Terraform to create an RDS MySQL account resource:
Parameter Description:
instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource
name: Account name, assigned by referencing the input variable account_name
password: Account password, prioritizes using account_password variable, uses randomly generated password if empty
11. Create RDS MySQL Database
Add the following script to the TF file to instruct Terraform to create an RDS MySQL database resource:
Parameter Description:
instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource
name: Database name, assigned by referencing the input variable database_name
character_set: Character set, assigned by referencing the input variable character_set, default value is "utf8"
12. Create RDS MySQL Database Privilege
Add the following script to the TF file to instruct Terraform to create an RDS MySQL database privilege resource:
Parameter Description:
instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource
db_name: Database name, assigned by referencing the input variable database_name
users: User privilege configuration block
name: Username, referencing the name of the previously created RDS MySQL account resource
readonly: Whether read-only privilege, set to true for read-only privilege
depends_on: Explicit dependency relationship, ensures database exists before privilege creation
13. Create RDS Backup
Add the following script to the TF file to instruct Terraform to create an RDS backup resource:
Parameter Description:
instance_id: RDS instance ID, referencing the ID of the previously created RDS instance resource
name: Backup name, assigned by referencing the input variable backup_name
depends_on: Explicit dependency relationship, ensures database privileges are configured before backup creation
14. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. At the same time, Terraform provides a method to preset these configurations through .tfvars files, which can avoid repeated input during each execution.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as
terraform.tfvarsfile in the working directory (this file name allows users to automatically import the content of thistfvarsfile when executing terraform commands; for other names,.autoneeds to be added before tfvars, such asvariables.auto.tfvars)Modify parameter values as needed
When executing
terraform planorterraform apply, Terraform will automatically read the variable values from this file
In addition to using terraform.tfvars file, variable values can also be set in the following ways:
Command line parameters:
terraform apply -var="vpc_name=my-vpc" -var="instance_name=my-instance"Environment variables:
export TF_VAR_vpc_name=my-vpcCustom named variable files:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.
15. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create resources:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming the resource plan is correct, run
terraform applyto start creating MySQL single instancesRun
terraform showto view the created MySQL single instance details
Reference Information
Last updated