How to setup and connect to Cloud SQL database in Google cloud platform from a Laravel application

How to setup and connect to Cloud SQL database in Google cloud platform from a Laravel application

Exploring the database Cloud SQL instance setup on Google cloud platform for backend applications

In this article, we'll be exploring database setup on Google cloud platform and integration example in a Laravel application using Cloud SQL.

This setup can be used as a database setup for any programming language or framework. In order to demonstrate the use case of the integration we'll use it in a Laravel application.

Without further redo let's get started with setting up the database on Google cloud platform (GCP)

  • Create a Google cloud platform (GCP) Account

If you don't have an account you can create one or login here

  • Prepare your project

After creating the account you should get this screen

image.png

Cloud SQL instances are fully managed, relational MySQL, PostgreSQL, and SQL Server databases. Google handles replication, patch management, and database management to ensure availability and performance.

Search for SQL or Cloud SQL instance on the dashboard

Screenshot 2022-10-20 at 23.20.35.png

  • Create instance

Screenshot 2022-10-20 at 23.11.12.png

  • Select a database engine as per your project requirement

Screenshot 2022-10-20 at 23.22.37.png

In this tutorial we'll choose SQL for demo purposes

In order to create an instance, you have to enable the Compute Engine API first

Screenshot 2022-10-20 at 23.23.50.png

Next fill in the details required to create an instance

Screenshot 2022-10-20 at 23.27.29.png

  • Instance ID

  • Password (preferably click generate)

  • Select the MySQL version for your application

  • Choose a configuration to start with

These suggested configurations will pre-fill this form as a starting point for creating an instance. You can customize as needed later.

  • Select region

  • Zonal availability (single or multiple)

Customize your instance

You can also customize instance configurations later

  • Connections

Choose how you want your source to connect to this instance, then define which networks are authorized to connect.

You can use the Cloud SQL Proxy for extra security with either option.

image.png

Instance IP assignment

  • Private IP

Configure private IP

  • Public IP

Select the instance IP assignment of your choice and define the network IP address

Screenshot 2022-10-20 at 23.44.19.png

Data Protection

Automated backups and point-in-time recovery

  • Click on create instance to continue

Instance dashboard

Screenshot 2022-10-20 at 23.45.42.png

  • Instance Summary

Screenshot 2022-10-20 at 23.46.14.png

  • Action Tab

Screenshot 2022-10-20 at 23.47.24.png

  • Database instance is ready

Screenshot 2022-10-20 at 23.53.22.png

click on databases on the left menu to create a database (kindly note this can also be done from a command line interface)

Screenshot 2022-10-20 at 23.54.00.png

  • Set the name for the database

Screenshot 2022-10-20 at 23.55.39.png

  • click on users in the left menu bar to create a user

image.png

  • Go to your project and use the credentials as per the programming language or framework requirement.

Below are different ways to connect to the Cloud SQL instance:

For more about connection options (from cloud run, cloud function, App engine, etc....) visit here

Let's do a demo with Laravel application to connect with the SQL instance from a local machine using proxy authentication.

**Don't use these instructions to set up the Cloud SQL Auth proxy for a production environment. **

The Cloud SQL Auth proxy is a Cloud SQL connector that provides secure access to your instances without a need for Authorized networks or for configuring SSL.

Here's the documentation on how it works

  • Requirements for using the Cloud SQL Auth proxy

To use the Cloud SQL Auth proxy, you must meet the following requirements:

  • The Cloud SQL Admin API must be enabled.

  • You must provide the Cloud SQL Auth proxy with Google Cloud authentication credentials.

  • You must provide the Cloud SQL Auth proxy with a valid database user account and password.

  • The instance must either have a public IPv4 address, or be configured to use private IP.

  • Enable the necessary Google Cloud APIs for the project.

image.png

Enable the Cloud SQL Admin API

After downloading the application rename it to cloud_sql_proxy.exe

  • Start the Cloud SQL Auth proxy

Start the Cloud SQL Auth proxy in its own terminal so you can monitor its output. Replace INSTANCE_CONNECTION_NAME with the instance connection name you copied in the previous step.

  • For Linux environments, use this command to launch the Cloud SQL Auth proxy:
./cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:3306

In PowerShell on Windows, use this command to launch the Cloud SQL Auth proxy:

.\cloud_sql_proxy.exe -instances=INSTANCE_CONNECTION_NAME=tcp:3306

image.png

  • Next, open the gcloud terminal or any cli and change directory to the location of the cloud_sql_proxy.exe file

image.png

  • Update the env file for the Laravel application
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=user_name
DB_PASSWORD="user_pwd"

Run the artisan migration command in the Laravel application directory CLI

php artisan migrate

image.png

  • Here's the activities when the artisan migrate was executed

image.png

  • We can use MySQL workbench to view the database record

image.png

If you're new to Google cloud platform or will like to explore other functionalities here are some articles i've published previously.

Thank you for reading this article.

Please kindly share with your network and feel free to use the comment section for questions, answers, and contributions.

Do you love this article?? please follow me on Hashnode alemsbaja or @alemsbaja to stay updated on more articles

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!