Help Center> Terraform> User Guide> Relational Database Service (RDS)> Creating an RDS MySQL DB Instance
Updated on 2022-02-10 GMT+08:00

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

huaweicloud_rds_instance

Procedure

  1. Plan and create a VPC, subnet, and security group.

    1. For details about how to create a network resource, see Configuring the Network.
    2. 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
      }

  2. 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 resources
    data "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
      }
    }

  3. 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"
    }

  4. Create resources.

    1. Run terraform init to initialize the environment.
    2. Run terraform plan to view resources.
    3. After you confirm that the resource information is correct, run terraform apply to start resource creation.
    4. Run terraform show to view information about the created RDS instance.

Table 1 Parameter description

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.

  • The value must be 4 to 64 characters in length and start with a letter. It is case-sensitive and can contain only letters, digits, hyphens (-), and underscores (_).

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. For details, see Regions and Endpoints.

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.

  • Value options: MySQL, PostgreSQL, and SQLServer

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.

  • The MySQL database port ranges from 1024 to 65535 (excluding 12017 and 33071, which are occupied by the RDS system). The default value is 3306.

volume

type

(Mandatory) Disk type of the database instance.

  • Options:

    ULTRAHIGH: SSD type

    ULTRAHIGHPRO: ultra-high I/O (advanced), which supports ultra-high performance (advanced) DB instances.

size

(Mandatory) Disk space of the database instance.

  • The value must be a multiple of 10 and range from 40 GB to 4,000 GB.