Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Wednesday 12 August 2020

Getting Started with Cloud SQL in Google Cloud Platform

In this article, we will learn How to get started with Cloud SQL in 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. 

Few Articles, which I already wrote and might be helpful for you if you are beginning with Google Cloud Platform are:

Let’s Begin:

Step 1: Log in with Google Cloud Platform by visiting (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 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 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, 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.
In the Next screen, Provide Instance ID and Root Password, Region, and zone in location. Select the Database version as per your need. For Demonstration, I am using MySQL 5.7 version. Click on Create button in order to create the DB Instance.
Within a few minutes, the MySQL Db instance will be ready to use. Click on 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 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 in getting started with Cloud SQL in Google Cloud Platform.


Post a Comment

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us


Email *

Message *

Like us on Facebook