In this post, we are going to set up Laravel 5.6 Database on top of Google App Engine. This post is the sequel to the previous post of Setting up Laravel 5.6 on Google App Engine that you can read from the link provided.
To set up Laravel Database, we are going to use Cloud SQL which is one of the services that Google Cloud has provided. With Google Cloud SQL, you have 2 choices of database vendors to choose from, MySQL or PostgreSQL. If you want to know more details about each of these databases, I recommend that you read their vast documentation.
Creating New Database Instance
The first thing that we have to do is create new Database Instance. You can choose either MySQL or PostgreSQL, but in this tutorial, I will be showing you how to install Laravel with the MySQL Client.
Once you are on the page, choose either of the engines, in my case, I choose the MySQL. Please note that MySQL provides 2 versions, First Generation and Second Generation. You should use the Second Generation as it’s their latest version and possibly the future MySQL version they will default the database instance with.
Upon pressing the database environment of your choice, you will be redirected to the next page. On this page, you can choose what the database will be used for, Development, Staging or Production. I will be choosing the Development environment for this tutorial and then move on to the next page.
On the configuration page, I will be setting it up for the most basic and go with the default configuration. In your case, do make sure to read each of the configurations properly if you plan to use it for the Staging or Production environment.
The other configuration options will be using the default one that has been provided. Create the instance and wait for sometime for it to complete.
Now press on the running instance and head to the Database tab. From this tab, you need to create a new database, I will call it laravel in this case. Please note that the default charset will vary depending on the content that your application will store.
Connecting to Cloud SQL
To test out our Laravel application with database, let’s run the command to scaffold the authentication
php artisan make:auth. Once you have done that, we need to edit the environment variables. The environment variables for App Engine won’t be the default .env file anymore but the one that’s provided inside the
env_variables: # Put production environment variables here. APP_LOG: errorlog APP_KEY: APP_KEY_HERE STORAGE_DIR: /tmp CACHE_DRIVER: database SESSION_DRIVER: database DB_HOST: localhost DB_DATABASE: laravel DB_USERNAME: root DB_PASSWORD: YOUR_DB_PASSWORD DB_SOCKET: "/cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME" beta_settings: cloud_sql_instances: "YOUR_CLOUDSQL_CONNECTION_NAME"
You can get the database password from when you create the new Cloud SQL instance previously. For the DB_SOCKET, you can get it by accessing the instance from the Cloud SQL Page and scroll down to see the details.
Once you have updated that, now we need to Conenct to Cloud SQL from External Applications and this will allow us to access the database within our local machine. Do follow the instructions that and once you have set it up, let’s connect our machine to the Cloud SQL.
Please note that you also need to enable the Google Cloud SQL API from the API Library section of Google Cloud.
Next, you will also need to generate Key ID from the IAM & admin for the App Engine default service account. Once you generate it, download the key as json and then save it somewhere secure. Run the
cloud_sql_proxy command to proxy the connection locally.
./cloud_sql_proxy -instances="mikateach-203901:us-east1:mikateach-mysql"=tcp:3307 -credential_file=LOCATION_OF_KEY_ID.json
Please note that the port will be 3307 as the default 3306 port has been used for my own local MySQL client. Now you need to edit the local .env file of the Laravel application so that you can run the migrations locally.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3307 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=
php artisan session:table php artisan migrate --force
The above code will Create a migration for the session database table and then migrate it. Finally, run
gcloud app deploy. Once it’s ready, you can launch the application by running
gcloud app browse.
If you have reached this far, well done! 🏆 for you. If somehow you are stuck somewhere, do comment below and let’s see what I can do for you.
As of now, you will have a working Laravel application that is running on top of Google App Engine with a database to store your application data. If you would like me to make further tutorial regarding Laravel with Google App Engine and it’s within the scope of my knowledge, do let me know by commenting down below.
I hope you managed to learn something new and if you find it useful, do share it with your friends as well. happy exploring!