In the previous post, our Laravel 5.7 application lands on the GAE (Google App Engine) Standard Environment.
In this post, we are going to connect Laravel 5.7 application to the Cloud SQL.
Cloud SQL is a fully-managed database system that supports PostgreSQL and MySQL databases in the GCP (Google Cloud Platform).
With Cloud SQL, I don’t need to install MySQL manually or maintain the database. It matches my goal of running Laravel application on GCP with no-ops.
Before you getting started, you need to prepare
1. Create a project in the Google Cloud Platform Console.
2. Enable billing for your project.
3. Enable the Cloud SQL API.
4. Install the Google Cloud SDK.
First of all, create a new instance of CSQL (Cloud SQL).
1. Login to your GCP console.
2. Search `sql` from the search bar.
3. Click on the CREATE INSTANCE button.
4. Select MySQL.
5. Choose the second generation.
6. Fill up all the field then click the Create button.
There you go! You have created a new instance of CSQL.
The beauty of a cloud server is you can connect anywhere.
To connect to the database, you connect the CSQL by using the Cloud SQL Proxy.
Follow the guide here to Install the proxy client on your local machine.
Connect to the CSQL instance via the proxy client.
If you have a local database which is running on the port number 3306, you can connect with another port number. In my case, I connect with port number 33060.
The CSQL is empty right now, let’s create a new database.
mysql -h 127.0.0.1 -u root -p -e "CREATE DATABASE running-laravel-on-gcp;”
In my case, I need to specify the port number 33060.
mysql -h 127.0.0.1 -P 33060 -u root -p -e "CREATE DATABASE running-laravel-on-gcp;”
Next step is run the database migration to create all the tables.
By setting database connection in .env, your local Laravel application connects to the CSQL instance directly.
If you don’t want to mess up with your local environment settings, you can run the database migration by passing the database connection as environment variables.
export DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=secret DB_SOCKET: "/cloudsql/running-laravel-on-gcp:asia-southeast1:running-laravel-on-gcp”
Okay! Run the migration now.
php artisan migrate
Then, deploy the application.
Like the setting in .env, you set the database connection parameters as environment variables in app.yaml.
# Put production environment variables here.
# Applicaton key
# Storage path
## Set these environment variables according to your CloudSQL configuration.
After the setting, run the deployment.
gcloud app deploy
Using root user in the production environment is not recommended, don’t mention about the simple password I set previously. Let’s change the root user's password and create a new user for your Laravel application.
You can manage your MySQL user accounts from GCP console.
1. From the console, select your instance then switch to USERS tab.
2. Create a new user named laravel and set a complex password.
3. After creating the user, you change the password of the root user account.
4. Then, update the app.yaml with the new credentials.
And, deploy it.
gcloud app deploy
Your Laravel application on GAE should work the same as previous.
Now, your Laravel 5.7 application is connected to the GSQL.
Laravel has an awesome filesystem which is powered by the Flysystem PHP package by Frank de Jonge.
Next post, I’m going to write about how to manage your filesystem on the cloud.