Creating an IBM Cloud® Databases for MySQL instance Tutorial
Objectives
This tutorial guides you through the process of deploying a IBM Cloud® Databases for MySQL instance and connecting it to a web front-end by creating a webpage that allows visitors to input a word and its definition. These values are then stored in a database running on IBM Cloud® Databases for MySQL. You will install the database infrastructure using Terraform and your web application uses the popular Express framework. The application can then be run locally, or by using Docker.
Getting productive
To begin the deployment process, install some must-have productivity tools:
- You need to have an IBM Cloud account.
- Node.js and npm - to install packages to and from public npm registries
- Terraform - to codify and deploy infrastructure
- Optional Docker - to run your application non-locally
Step 1: Obtain an API key to deploy infrastructure to your account
Follow these steps to create an IBM Cloud API key that enables a program or script without distributing your password to the script. You can create up to 20 API keys.
For security reasons, the API key is only available to be copied or downloaded at the time of creation. If the API key is lost, you must create a new API key.
Step 2: Clone the project
Clone the project from the Cloud Databases Hello World project GitHub repository.
git clone https://github.com/IBM-Cloud/clouddatabases-helloworld-examples.git
Step 3: Install the infrastructure
-
Open the mysql folder in the GitHub tutorial repository.
-
Create a document named
terraform.tfvars
with the following fields:ibmcloud_api_key = "<your_api_key_from_step_1>" region = "<your_region" admin_password = "<make_up_a_password>"
The
terraform.tfvars
document contains variables that you may want to keep secret so it is ignored by the GitHub repository. -
Install the infrastructure with the following command:
terraform init terraform apply --auto-approve
The Terraform script will output configuration data that is needed to run the application, so copy it into the root folder:
terraform output -json >../config.json
Step 4: Run your app locally
-
To connect to the database from your local machine, ensure that you are in the MySQL database, then install the node dependencies and run the service with the following command:
npm install npm run start
If successful, the output will show you are connected:
#Connected! #Server is listening on port 8080 Open a browser and visit http://localhost:8080
A welcome page with a database logo will be displayed in your browser window.
-
To test the interface, enter a word and its definition. The data pair is added to the database and appears in a list at the bottom of the page.
Step 5 (optional): Run the app from a Docker container
The first step towards hosting your application from a service like Code Engine is to containerize the app code inside a Docker container and run it from there.
Make sure you are logged into your Docker account. In the MySQL database, enter the following command:
docker build -t database-hello-world:1.0 .
docker run -p 8080:8080 database-hello-world:1.0
Visit http://localhost:8080 to see the same page from the previous step.
Congratulations, you've created an app with a front-end that feeds data into your IBM Cloud® Databases for MySQL deployment!