Create an SQL instance, Create a table, and Load Data
This page shows you how to create and connect to MySQL instance and perform basic SQL
operations using the Google Cloud Console and a client.
Set up your project
Before performing the steps in this tutorial, complete the following tasks:
1. In the Google Cloud Console, on the project selector page, select or create a Google
Cloud project
**NOTE: if you do not plan to keep resources that you create in this procedure, create a
project instead of selecting an existing project. After you finish these steps, you can
delete the project, removing all resources associated with the project.
2. Enable the Cloud SQL Admin API.
Note: To use this API you will need to Click on ‘Create Credentials’
• Select an API
• Cloud SQL Admin API
• What data will you be accessing?
• User data
• Click Done
Note: If you are running a local instance of MySQL, stop it before beginning to work with
a Cloud SQL instance. Otherwise, you might encounter errors such as address already in
use.
Create a Cloud SQL instance
In this procedure, you will use the Google Cloud Console (use the gcloud command-line tool,
curl, or PowerShell).
1. In the Google Cloud Console, go to the Cloud SQL Instances page (you can type SQL in
the search bar)
2. Click Create Instance
3. Click MySQL
4. If you’re prompted to enable the Compute API, click the Enable API button.
5. Enter myinstance for Instance ID
6. Enter a password for the root user. Be sure to remember the password as you will need it
to connect to the instance
7. There are other options you may set. Keep an eye on the Pricing estimate since it
changes significantly depending on your choices. See my different choices and pricing
changes below
Option 1:
Option 2:
Option 3: (I selected this one)
8. Under Data Protection – unclick Enable deletion protection
9. Click Create
You are returned to the instances list; you can click into the new instance right away to
see the details, but it won’t be available until it initializes and starts (This can take a
little while).
10. Click on the instance and click on the start button in the top of “overview”
Create a database and upload data
1. There are several ways to connect to a MySQL instance. In this quick introduction, we
will use GCP cloud shell
2. Create an SQL database on your Cloud SQL instance (I changed my instance name but you
can still use myinstance)
3. On the instance overview screen, under “Connect to this instance” section, select
Connect using Open Cloud Shell
4. Once the Cloud Shell has been activated, you should see a screen similar to the following
5. Press the enter key to run the command:
gcloud sql connect <your-instance-ID> - user=root -quiet
6. You will be asked to enter your password from the instance you created in the console.
*NOTE: You will NOT see the password on the screen. After you enter the password, hit
enter
7. We will now create a new database “customers” and table “entries”. After each line
below hit enter
CREATE DATABASE customers;
USE customers;
CREATE TABLE customers
-> (fName VARCHAR(255),
-> lName VARCHAR(255),
-> phone VARCHAR(255),
-> address VARCHAR(255),
-> city VARCHAR(255),
-> state VARCHAR(255),
-> model VARCHAR(255),
-> comments VARCHAR(255));
You should see a screen like this:
8. Now you will enter data into your newly created table. Enter the following commands:
INSERT INTO customers (fName, lName, phone, address, city, state, model, comments)
VALUES (‘Tony’, ‘Barone’, ‘555-676-7778’, ‘1018 State Street’, ‘Houston’, ‘TX’, ‘A-1237’,
‘This is the best product I have ever purchased.’);
INSERT INTO customers (fName, lName, phone, address, city, state, model, comments)
VALUES (‘Helen’, ‘Smith’, ‘777-878-0098’, ‘889 Elm Road’, ‘St. Louis’, ‘MO’, ‘H-435’, ‘I
would never buy this product again!’);
INSERT INTO customers (fName, lName, phone, address, city, state, model, comments)
VALUES (‘Susan’, ‘Heller’, ‘876-888-6795’, ‘879 Main Street’, ‘Los Angeles’, ‘CA’, ‘K-8887’,
‘All good’);
INSERT INTO customers (fName, lName, phone, address, city, state, model, comments)
VALUES (‘Betsy’, ‘Clark’, ‘555-887-1098’, ‘45 West 54th Ave’, ‘Topeka’, ‘KS’, ‘Z-2’, ‘No
Issues’);
You should see a screen like this:
9. Retrieve the data:
SELECT * FROM customers;
The result is:
Cleanup
1. In the Google Cloud Console, go to the Cloud SQL Instances page.
2. Select your instance’s name to open the Overview page
3. In the icon bar at the top of the page, click Delete
4. In the Delete Instance window, type your instance’s name, then click Delete to delete the
instance
You cannot reuse an instance name for about 7 days after the instance deleted
Information obtained from (with some modification:
https://cloud.google.com/sql/docs/mysql/quickstart
Under use by Attribution 4.0 International (CC BY 4.0)
For additional help, please refer to https://cloud.google.com/sql/docs/mysql/quickstart