Creating an RDS MySQL DB Instance
Application Scenario
MySQL is an open-source relational database management system. The LAMP solution (Linux + Apache + MySQL + Perl/PHP/Python) makes it much efficient to develop web applications. This section describes how to create an RDS MySQL DB instance by using Terraform scripts.
Related Resources
Procedure
- Plan and create a VPC, subnet, and security group.
- For details about how to create a network resource, see Configuring the Network.
- If you want to use a created network resource, use data source to obtain the corresponding resource ID. The following is an example:
data "huaweicloud_vpc" "myvpc" { name = var.vpc_name } data "huaweicloud_vpc_subnet" "mysubnet" { vpc_id = data.huaweicloud_vpc.myvpc.id name = var.subnet_name } data "huaweicloud_networking_secgroup" "mysecgroup" { name = var.secgroup_name }
- Create an RDS MySQL DB instance.
Example 1: Using new network resources and a random password
data "huaweicloud_availability_zones" "myaz" {} resource "random_password" "mypassword" { length = 12 special = true override_special = "!@#%^*-_=+" } resource "huaweicloud_rds_instance" "myinstance" { name = "mysql_instance" flavor = "rds.mysql.c2.large.ha" ha_replication_mode = "async" vpc_id = huaweicloud_vpc.myvpc.id subnet_id = huaweicloud_vpc_subnet.mysubnet.id security_group_id = huaweicloud_networking_secgroup.mysecgroup.id availability_zone = [ data.huaweicloud_availability_zones.myaz.names[0], data.huaweicloud_availability_zones.myaz.names[1] ] db { type = "MySQL" version = "8.0" password = random_password.mypassword.result } volume { type = "ULTRAHIGH" size = 40 } }
Example 2: Using existing network resourcesdata "huaweicloud_availability_zones" "myaz" {} resource "huaweicloud_rds_instance" "myinstance" { name = "mysql_instance" flavor = "rds.mysql.c2.large.ha" ha_replication_mode = "async" vpc_id = data.huaweicloud_vpc.myvpc.id subnet_id = data.huaweicloud_vpc_subnet.mysubnet.id security_group_id = data.huaweicloud_networking_secgroup.mysecgroup.id availability_zone = [ data.huaweicloud_availability_zones.myaz.names[0], data.huaweicloud_availability_zones.myaz.names[1] ] db { type = "MySQL" version = "8.0" password = var.rds_password } volume { type = "ULTRAHIGH" size = 40 } }
- Configure variables.
Create the variables.tf file, enter the following information, and save the file. You can change the variable values based on your needs.
variable "vpc_name" { default = "vpc-basic" } variable "vpc_cidr" { default = "172.16.0.0/16" } variable "subnet_name" { default = "subent-basic" } variable "subnet_cidr" { default = "172.16.10.0/24" } variable "subnet_gateway" { default = "172.16.10.1" } variable "primary_dns" { default = "100.125.1.250" }
- Create resources.
- Run terraform init to initialize the environment.
- Run terraform plan to view resources.
- After you confirm that the resource information is correct, run terraform apply to start resource creation.
- Run terraform show to view information about the created RDS instance.
Resource Name |
Parameter |
Description |
|
---|---|---|---|
huaweicloud_rds_instance |
name |
(Mandatory) Database instance name. Under the same tenant, database instances of the same type can have the same name.
|
|
flavor |
(Mandatory) DB instance flavor. In this example, rds.mysql.c2.large.ha is used. You can query the instance flavor via huaweicloud_rds_flavors. |
||
ha_replication_mode |
(Optional) Replication mode for the standby DB instance. For MySQL, the value can be async or semisync. |
||
availability_zone |
(Mandatory) AZ where the instance is located. Multiple AZs are supported for master/standby instances. |
||
vpc_id |
(Mandatory) ID of the VPC to which the instance belongs. |
||
subnet_id |
(Mandatory) ID of the subnet to which the instance belongs. |
||
security_group_id |
(Mandatory) ID of the security group to which the instance belongs. |
||
db |
type |
(Mandatory) Database engine type.
|
|
version |
(Mandatory) Database engine version. For MySQL, versions 5.6, 5.7, and 8.0 are supported. |
||
password |
(Mandatory) Database password. The value contains 8 to 32 characters. Only letters, digits, and the following special characters are supported: ~!@#%^*-_=+? Enter a strong password to prevent security risks such as brute force cracking. |
||
port |
(Optional) Database port.
|
||
volume |
type |
(Mandatory) Disk type of the database instance. |
|
size |
(Mandatory) Disk space of the database instance.
|
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.