Installing Django 3.0.4 from scratch using Google Cloud MySql as database on MacOs Catalina 10.1 5 [Step-by-Step tutorial]

Pedro Mejía
5 min readMay 16, 2020

Goal:
Set up a running Django 3.0.4 “Hello World” app that uses MySql on Google Cloud.

Requirements:
MacOS V.10.15.3
Python 3.7.7

  1. Create a a GCP MySql 3.7 instance in the console :

Go to https://console.cloud.google.com go to menu ( ☰ ) → SQL

Console SQL

Click in “Create Instance”

Select MySQL:

Give it a name and generate a root password. Save the password somewhere safe, then under the “configuration options → machine type and storage” select the following configuration (db-f1-micro):

Click create.

2. Locally (meaning: “in your own computer”), install the latest Google Cloud Software development kit, ( AKA: “gcloud SDK”) following this instructions.

3. Open the terminal and execute while on root (to be on root write “cd” and press enter):

gcloud auth application-default login

The browser should open, login, and select the project where you deployed the MySql server when executing the first step.

4. Install the Cloud SQL Proxy locally, and enable the Cloud SQL Admin API through the Google Cloud console. The Google Cloud console is the interface that happens when one clicks on the “console button” on the top right corner of https://cloud.google.com).

Go to your local terminal and in the root folder and download the proxy by doing:

curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64

Then, make the proxy executable:

chmod +x cloud_sql_proxy

5. Create a service account:

In the cloud console (https:// console.cloud.google.com) go to the navigation menu ( ☰ ) → IAM & Admin → Service Accounts, Select your project and click “Create Service Account”. On the new dialog, name the service account: “SQL Service Account” and Click “Create”, then select the appropriate role: Client, Editor or Admin (For this tutorial I’ll select “Admin”), and click continue. Click “Create Key” and select JSON. Save the downloaded JSON file on a safe place. Further details if needed can be found here.

You need to install the Cloud SQL Proxy because you need a way to redirect the requests from localhost to the newly created MySql instance. The Cloud SQL proxy is a software that takes the requests made to localhost and forwards them to the MySQL instance on the Google Cloud Platform (AKA: GCP).

6. To run the proxy, you need your instance connection name. To get it go to the the Google Cloud console, and write “SQL” on the search box and click the result pointed with the blue arrow:

Console Search Box

There will be a list with the deployed instance. The connection name is under the column named “Instance connection name”. In the next image is marked with the green square.

7. Go to the terminal and execute the following command. (Substitute [yourInstanceName] with what is in green in the previous image (no brackets in the command)

./cloud_sql_proxy -instances=[yourInstanceName]=tcp:3306

If you have mySql installed locally, its default port is 3306, hence you must stop it first before running the proxy or you will get this error:

listen tcp 127.0.0.1:3306: bind: address already in use

If everything is okey, your local shell console should print:

Listening on 127.0.0.1:3306 
Ready for new connections

Now lets do the Django Part:

Open a new local shel terminal window while on root (do “cd” to be on root)

python3 -V

It should return:

Python 3.7.7

Create a Python environment by doing, “env-name” can be changed for a name of your choice, in this tutorial we stick to “env-name”:

python3 -m venv env-name 

Activate the environment:

source env-name/bin/activate

You should see now the “env-name” in your terminal.

Install pip:

pip install --upgrade pip

Install the dependencies: django and PyMysql, do:

pip install django
pip install PyMySQL

Go to the folder where you will work and create a django Project (In this tutorial we do it on root, be on root by doing “cd” on your local terminal shell).

django-admin startproject project

project” can be changed to the name of your choice, we stick to “project” in this tutorial.

Open the folder on your favorite python editor and leave it open. I personally use PyCharm by Jetbrains.

We return to the Google Cloud Console (https://console.cloud.google.com) in your browser and then go to the menu ( ☰ ) → SQL, click on the name of your instance and in the left menu click “databases”

Click “create database”, select a name and click “create”

Then create a new user, go to the left menu and select users:

Click “create user account”, as name select: “app-1”(You can change that, in this tutorial we stick to “app-1”) and a password (referenced as [MySql-user-password] down bellow).

Go to the Python editor in the project folder previously created, and open the __init__.py file and add:

import pymysql
pymysql.version_info = (1, 3, 13, "final", 0)
pymysql.install_as_MySQLdb()

Save it and open the “settings.py” file and update de Databases array with the following:

'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'app-1',
'PASSWORD': '[MySql-user-password]',
'HOST': '127.0.0.1',
'PORT': '3306',
}

Return to the local shell terminal, to the project folder: (“cd project” while on root), and do:

./manage.py makemigrations
./manage.py migrate

When the command is executed, you may check the local terminal window that had the Cloud SQL proxy running routing all the connections to the instance. Now you can start developing in Django and your database will be running on Google Cloud SQL.

Bravo! you are done :)

--

--