7.2 Deploy MySQL DB System with Terraform and access the system
This tutorial is a continuation of Deploy MySQL DB System with Terraform (basic tutorial).
I strongly advise you to read it before moving on to this one.
As we have mentioned in the previous tutorial, accessing the MySQL DB System from MySqlOciVCN has been proven to be troublesome:
root@deploymentmachine:/home/oci_mysql# mysqlsh root@10.0.1.3
Please provide the password for 'root@10.0.1.3': ***********
MySQL Shell 8.0.23
Copyright (c) 2016, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a session to 'root@10.0.1.3'
MySQL Error 2003 (HY000): Can't connect to MySQL server on '10.0.1.3' (110)
cannot reach mysql from deploymentmachine.
If we want to access the MySQL DB System, as a work around, we have to deploy an instance on which mysql-shell will be installed (see below diagram)

Adapting the Terraform code
From previous tutorial, we have 11 files that make possible the creation of a MySQL DB System within a VCN, in a different compartment.
We now need to provision an instance in same VCN and compartment as the MDS, to which we will assign a public IP address.
In order to proceed with the creation of a new instance, a few more variables must be added, such as the instance name, the instance shape, the image of the OS and the path to the ssh private keys.
Addition to file variables.tf
The new fileinstance.tf will have the following content:
We also need to change the data.tf file, by adding a couple of data sources, for extracting the primary VNIC, and obtaining the Public IP of the instance.
The file data.tf will now look like this:
... and, as a last step, we need to install mysql-shell on that instance. We will go ahead and implement this by using "remote-exec" provisioner (since we are using this provisioner, we can also perform the necessary updates, and setup the firewall rules)
The new file remote.tf will have the following content:
Now, my folder has a total of 13 files:
Code example
The code example is at Github repository: https://github.com/MuchTest/MySqlDbSystemExample1
Comments are added for guidance - you will need to provide certain info if you want the environment to work (tenancy ocid, user ocid ... etc). Find a "how to" guide at this tutorial from Chapter 1.
Checking the deployed MySQL DB System in Oracle Cloud UI
Deploy the environment by using the main terraform commands:
terraform initterraform planterraform apply
When you run the command "terraform apply", the creation of instance "MySqlShellInstance", respectively MySQL DB System will start immediately:


When the instance is launched, the provisioner "remote-exec" will perform the necessary steps for installing and configuring the mysql-shell.
Output from "terraform apply", when mysql-shell is installed:

As you are probably used to it, the creation of a MySQL DB System might take a while (~15min or more, depending on the resources you have chosen for its creation)
Access the MySQL DB System
As soon as both systems are provisioned, try to ssh into the new instance (I mentioned the public IP in these examples because everything will be deleted soon)
... once you are in, from "mysqlshellinstance", try to reach the MySQL DB System via mysql-shell (in this example, the MySQL DB has 10.0.1.4 as IP address):
Destroy resources
To destroy resources, use the command "terraform destroy"
Last updated