SQL on The Cloud With Google Cloud and Python | Towards Data Science

Everyone and their dog uses the cloud nowadays — and with good reason. It takes what used to be a tricky task and makes it as easy as falling off a log.
Developing, configuring, and hosting anything on the internet was never simple. Now imagine you need to worry about security threats that evolve daily, world-wide latency, infrastructure scaling, service outages, and much more.
But now, we can have databases, web-apps, IoT hubs, and much more built quicker than I can make a coffee — all thanks to the cloud.
These new cloud services come with the ability to be hosted almost anywhere in the world, with top-tier security, automatic scaling, and guarantees of 99.999% availability. The cloud has transformed tech.
We will cover the steps we need to take to set up a MySQL database with Google Cloud Platform (GCP) and looking at how we can query that database with Python.
The first thing we need to do is create our database. For that, we head over to the Cloud SQL Instances page.
Here, we have two options — use an existing project or create a new one. If you don’t already have a project setup, go ahead and create one with Create Project now.
On the next page, we will click Create Instance, which takes us to a choice of MySQL, PostgreSQL, and SQL Server — we will be using MySQL. Feel free to use another option if you prefer.
Next, we set up our instance info; this is pretty straightforward — choose your or your customer’s closest region, and remember your password (we need it when connecting with Python later).
Now we should be taken through to our database overview. Instance creation will usually take a few minutes.
Head back to our instance details page (you can access it by clicking on the Instance ID here).
We click on the Connections tab (on the left-taskbar) and click + Add network under Public IP. Give the network a name and enter the IP address range you would like to allow — 0.0.0.0/0 allows entry to all IP addresses.
Of course, this is a pretty big security issue, which is why we use SSL encryption to restrict access to our instance.
While still on the Connections tab, we scroll down to the SSL section. Click Allow only SSL connections and then Create a client certificate — download all three .pem files — we will use them when connecting with Python.
Next, we connect to our instance with Python.
First, we need to install the MySQL connector library:
pip install mysql-connector-python
There are a few things we need to set up our connection:
Username — this should be root.
Password — we set this up earlier.
Host — the public IP address of our SQL instance, we can find it on our Cloud SQL Instances page.
SSL certification — the three files, server-ca.pem, client-cert.pem, and client-key.pem.
All of these are used in our config variable, which we use to establish our connection like so:
We’re now connected to our Cloud MySQL instance with Python!
Now we’ve set everything up, let’s upload some data and run some quick queries.
First, we need a database to hold our data — we use our previously setup connection cnxn:
Now we connect to testdb by adding database: testdb to our config dictionary and connecting just like we did before:
Back in our Instance Details window, we can see our new testdb database in the Databases tab. We can also create new databases from here by clicking Create Database.
We will use the All Space Missions from 1957 dataset on Kaggle. You can download a cleaned CSV of the data from here.
We create a new table called space_missions with the correct column headers using cursor.execute:
Next, we insert our data:
Now our database should contain the full space_missions table. We can test this by querying for the top five rows of our table — which should match the top five we saw earlier:
The output is the same as what we saw earlier, which means we now have our data successfully stored in the cloud.
Any further queries or transformations can be performed as we usually do for MySQL.
Now we have a fully functional MySQL database with SSL encryption. Connecting to our database from anywhere in the world requires no more than four lines of Python code.
From then, we can query our database as we would any other — it’s incredibly easy to pick up and use.
Let me know if you have any questions, feedback, or ideas over on Twitter or in the comments below. Thanks for reading!
Interested in learning more about Google Cloud? You might like my previous article covering API deployment with Docker and GCP: