Getting Started With Cloud SQL In Google Cloud Platform

Introduction 

 
In this article, we will learn how to get started with Cloud SQL in the Google Cloud Platform. We will start with the creation of a VM Instance. Then, we create a MySQL DB in the Cloud SQL and access that DB in the Web application hosted in the VM Instance created in the Google Cloud Platform.
 
 
 
There are a few articles that I already wrote and might be helpful for you if you are beginning with Google Cloud Platform:
Let’s begin.
 
Step 1
 
Log in to the Google Cloud Platform by visiting https://console.cloud.google.com/ (Google provides $300 as a free trial, but for that, you need to link your credit card).
 
 
Once logged in you will be redirected to Google Cloud Dashboard, as shown in the below image:
 
 
Step 2
 
Let’s add a Virtual Machine Instance in the Google Cloud Platform by clicking on the VM Instances sub-menu shown in the Compute Engine menu. 
 
 
Click on the Create button in order to create a new VM Instance.
 
 
Give a name to your VM Instance, select Region, Zone, Machine Configuration as per your/client business need.
 
 
Select a boot disk for your operating system. For the demonstration, I am using Debian, a Linux based operating system.
 
 
 Allow HTTP traffic for the VM so that the application hosted in the VM can be accessed through the internet network.
 
 
Click on Management, Security, Disks, Networking, Sole tenancy in order to expand it. I want to add a Start-up script to Install Apache, PHP, and modules required to access MySQL in PHP.
 
 
Click on the create button in order to create a VM Instance.
 
 
Within a minute, the VM Machine instance is ready to use. The green tick icon will be shown before the name once VM is ready.
 
 
Step 3
 
Let’s create a MySQL DB instance in Cloud SQL. Click on the SQL menu option available in the Google Cloud Platform.
 
 
Cloud SQL instances are fully managed, relational databases for which google handles replication, patch management, and database management to ensure availability and performance. Currently, MySQL, PostgreSQL, and SQL Server RDBMS are available in Cloud SQL. Click on Create Instance button.
 
 
Choose MySQL DB, as shown below.
 
 
On the Next screen, Provide Instance ID and Root Password, Region, and zone in location. Select the Database version as per your need. For this demonstration, I am using MySQL 5.7 version. Click on the Create button in order to create the DB Instance.
 
 
Within a few minutes, the MySQL DD instance will be ready to use. Click on the Instance name.
 
 
Step 4
 
Click on Users menu in Cloud SQL in order to create a User Account. Click on Add User Account link as shown in the below image.
 
 
Provide a username and password in order to create the user for the DB Instance.
 
Step 5
 
Copy the external IP Address of the VM Instance created to host the Web Application.
 
 
Now, click in the Connections menu of Cloud SQL in order to authorize the External IP to access the DB Instance.
 
 
Provide the name and the IP of the VM Instance copied in the above steps. Click on the Save button in order to whitelist the VM Instance IP Address.
 
 
Step 6
 
Click on SSH in order to connect to the VM Instance.
 
 
 
 
Change the current directory to /var/www/html
 
 
Create an index.php file with the nano command-line based text editor by typing the below command:
 
sudo nano index.php
 
 
Add the below code in order to test whether the application is able to connect with the MySQL DB instance by providing DB Instance details like DB Server IP, Db User, Db Password. If the connection is created successfully, then we will show Database connection succeeded else Database connection failed message. Press CRTL+O and Enter to save the file.
 
 
Now restart the apache server with the below command in the SSH.
 
 
Open the external IP Address of the VM in which Web application is hosted in the web browser. As you can clearly see that Our web application is connected successfully with the MySQL DB Instance.
 
I hope this article will help you get started with Cloud SQL in the Google Cloud Platform.


Similar Articles