7.1 Deploy MySQL DB System with Terraform (basic tutorial)

This tutorial covers:

  • Terraform documentation for MySQL OCI (explained for this example)

  • Example of provisioning a MySQL OCI service (new compartment, with a VCN and public subnet)

Prerequisites:

For networking documentation (along with Terraform examples), check my tutorials on VCN (basics)

For an introduction into MySQL DB System in OCI, check my tutorials "The basics - OCI UI (MySQL DB System)", "The basics - OCI CLI (MySQL DB System)", respectively "Access MySQL DB System"

Getting familiar with Terraform for MySQL OCI

Resources (for this example)

Resource oci mysql mysql db system- creates and launches the MySQL DB.

The required fields of oci mysql mysql db system - provide the admin user, the admin's password, the shape, the storage capacity (GB), the compartment, the AD and subnet OCID

admin's password password must be between 8 and 32 characters long, and must contain at least 1 numeric character, 1 lowercase character, 1 uppercase character, and 1 special (nonalphanumeric) character.

If you do not offer a proper password, the creation of the instance will fail

shape_name refers to the type of instance on which you want to deploy the MySQL service.

data_storage_in_gb refers to data volume in giga that must be created and attached to the instance

These requirements can be set-up as variables:

The resource oci_mysql_mysql_db_system will be applied in the create_mysql.tf file:

Data sources (for this example)

Data source oci mysql mysql configurations- provides the available configuration for when creating a database system.

The required fields of oci mysql mysql configurations: one needs to provide the compartment OCID, the shape_name and the type.

shape_name refers to the type of instance on which you want to deploy the MySQL service.

typerefers to either a "DEFAULT" configuration or a "CUSTOM" configuration.

We will provision the MySQL service on a VM.Standard.E2.1 instance, with DEFAULT configuration, in data.tf file:

Code example

The files under oci_mysql folder:

The content of variables.tf

The provider.tf (just in case)

1. For compartment & networking configuration

The content of compartment.tf

The content of internet gateway, int_gateway.tf

The content of vcn.tf:

Content of dhcp_opt.tf:

The content of route.tf

The content of subnet.tf

The security lists, security_list.tf:

2. For MySQL OCI configuration

The content of data.tf:

The content of create_mysql.tf

Checking the deployed MySQL OCI in Oracle Cloud UI

Deploy the environment by using the main terraform commands:

  • terraform init

  • terraform plan

  • terraform apply

The deployment of the MySQL OCI service may take a while (around 15min in this case).

In the meantime, you can check in the OCI UI if the compartment has been created, along with the configuration in progress for MySQL OCI:

Go to Menu > MySQL > DB Systems

...and choose the compartment MySqlOciCompartment from List Scope:

As soon as all resources are created, the MySQL DB will be in Active status:

Destroy resources

To destroy resources, use the command "terraform destroy"

Discussion

This tutorial was meant to be a brief introduction in Terraform and MySQL DB System.

If you try to reach the MySQL DB from your machine of deployment, it will obviously fail:

Work around on this subject in next tutorial

Last updated