Cloud SQL is an online managed database service that allows the administering of your relational databases on Google Cloud Platform. Databases offered in this platform are MySQL and PostgreSQL.
This how-to walks you how to create and connect to a Cloud SQL instance and perform basic SQL operations using the Google Cloud Platform Console and the MySQL client.
Overview
SQL queries perform tasks such as update records on a database or retrieve records from a database. Google Cloud offers MySQL as a cloud database.
Google Cloud SQL offers access to MySQL client through a cloud shell where you can run all MySQL queries and manage the database.
Ensure you Sign in to your Google Account, and in case you do not have one, sign up for a new account after logging in, Select or create a GCP project and make sure you enable billing for your project.
How to get started with Cloud SQL for MySQL
Create Cloud SQL instance
Select your project and click Continue.
Then, Click Create Instance as shown below;
Choose a database engine. Thus, Click MySQL.then click as shown below;
There are types of MSQL instances in our case we use Second Generation; then Click Choose Second Generation.
Enter a name for your Instance ID. The choice is permanent and ensures you use lowercase letters, numbers and hyphens and always start with a letter.
Set a root password and select the Region for your instance which should be the same region as the resources that access it.
Under Configuration options, update any other settings you need for your instance, Use the default values for the other fields. Click Create.
Connect to MySQL client using cloud Shell
Login to Google Cloud Platform Console, click on Cloud Shell () to initialise Cloud Shell.
When Cloud Shell finishes initialising, you should see:
Click on the Cloud Shell prompt, type the below command to connect to your Cloud SQL instance:
gcloud sql connect myinstance --user=root
My instance name is mysqlateam. Replace this in the syntax;
gcloud sql connect mysqlateam --user=root
Type the root password to access MySQL shell;
Create a database and Insert records
Create MySQL database
CREATE DATABASE AteamTest;
Insert data into the Ateamtest database:
USE AteamTest; CREATE TABLE entries (AteamTest VARCHAR(255), content VARCHAR(255), entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID)); INSERT INTO entries (AteamTest, content) values ("Team Kenya", "Karibu Kenya"); INSERT INTO entries (AteamTest, content) values ("Hakuna Matata", "Enjoy a Safari");
To view data from the database;
SELECT * FROM entries;
You should see:
+—————+—————-+———+
| AteamTest | content | entryID |
+—————+—————-+———+
| Team Kenya | Karibu Kenya | 1 |
| Hakuna Matata | Enjoy a Safari | 2 |
+—————+—————-+———+
2 rows in set (0.11 sec)
Clean up
If testing Cloud SQL, remember to delete the instance to avoid incurring charges from Google. For a production environment, check the pricing from here.
Navigate to Cloud SQL instance and select the instance you want to delete. On the far end, click on the three dots. Select Delete.
Conclusion
In this how-to, we have covered how to create a Cloud SQL instance that runs MySQL and created a database in which we inserted and retrieved records. Having set up this, you can now enjoy running MySQL queries and run your database with ease.
Check out these top 3 Cloud hosting services:
- Get the best cloud hosting and secure your valuable data.