Redeem and use your Google coupon. Do not enter your credit card. (If you have not received an email with the link to redeem a coupon, please notify the course instructor via email.)
This will prompt a screen allowing you to create a new project.
Enter your Project Name (e.g., webPL
),
be sure the Billing account is associated with
the Google coupon you redeemed. Then, click CREATE.
Note: depending on how your account was set, you may be required to enter Location or Organization. Please select University of Virginia and proceed to create the project.
indicates that the
SQL instance has been started.
indicates that the
SQL instance has not been started.
The SQL instance must be started before databases can be created or SQL commands can be run.
Remember to stop the SQL instance when not in use.
There are several ways to connect to a MySQL instance. In this quick introduction, we will use GCP cloud shell.
gcloud sql connect <your-instance-ID> --user=root --quiet
You may be asked to authorize cloud shell.
If you receive an error Your current active account [ACCOUNT] does not have any valid credentials,
you need to activate your account by running the command
gcloud config set account <your-GCP-account>
.
If you receive a permission denied error, click the link specified in Cloud Shell to enable Cloud SQL Admin API.
Then, try running
gcloud sql connect <your-instance-ID> --user=root --quiet
again.
Once the connection has been established, you should see the mysql> prompt.
Let's create a guestbook database. To create a database, there are several options.
You may use the Create database feature of Google Cloud Console.
Once a database has been created, you should see a screen listing your database.
To delete a database, click the three-vertical-dots option, then select Delete
You may run a SQL command to create a database.
CREATE DATABASE guestbook;
Note: SQL commands are not case sensitive. This example uses uppercase and lowercase simply to make it easy to read.
There are several ways to create a table. In this quick introduction, we will use GCP cloud shell.
USE guestbook; CREATE TABLE entries (guestName VARCHAR(255), content VARCHAR(255), entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID));
You may run DESC entries;
to view the table schema (structure).
There are several ways to insert data into a table. In this quick introduction, we will use GCP cloud shell.
USE guestbook;
Note: if you have previously specified the database, no need to include this code.
INSERT INTO entries (guestName, content) values ("Humpty", "Humpty's here!"); INSERT INTO entries (guestName, content) values ("Dumpty", "Dumpty's here too!");
There are several ways to retrieve data from a table. In this quick introduction, we will use GCP cloud shell.
USE guestbook;
Note: if you have previously specified the database, no need to include this code.
SELECT * FROM entries;
To disconnect from the mysql terminal (monitor), run the exit
command.
To disconnect from the Cloud Shell, run the exit
command.
CREATE DATABASE friendbook; USE friendbook; CREATE TABLE friends (friendName VARCHAR(255), phone VARCHAR(255), entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID)); INSERT INTO friends (friendName, phone) values ("Humpty", "111-111-1111"); INSERT INTO friends (friendName, phone) values ("Dumpty", "222-222-2222");
If you have not created a storage bucket, please refer to Create GCP storage bucket.
Upload friendbook.sql to your GCP bucket
You may need to click ">" to navigate to the desired file.
Assuming that your friendbook.sql file is in a bucket named webpl-demo.
Then click the Select button.
If you wish to execute a .sql on an existing database, select a destination database. Then, click Import.
To verify that your SQL file has been run successfully, connect to the instance using the Cloud Shell. Run the following code
USE friendbook; SELECT * from friends;You should see two records of data entered into your newly created friends table.
If you have not created a storage bucket, please refer to Create GCP storage bucket.
Assume the exported file will be called Cloud_SQL_Export-friendbook.sql and stored in webpl-demo bucket.
Click the Select button, and then Export.
To retrieve the exported file, go to your GCP console — Storage's Browser screen.
Go to GCP console, on the instance overview screen, click the STOP option.
Note: the SQL instance must be stopped to avoid incurring charges.
Released under the
CC-BY-NC-SA 4.0 license.