In this lab, you use Cloud SQL to create databases that are managed by Google. You create both PostgreSQL and MySQL databases, and you connect to those databases using the Google Cloud CLI and from a virtual machine.
Objectives
In this lab, you learn how to perform the following tasks:
Create a Cloud SQL PostgreSQL database.
Connect to the database using the Cloud SDK.
Create a Cloud SQL MySQL database using the CLI.
Connect to the MySQL database from a virtual machine.
Setup and requirements
In this task, you use Qwiklabs and perform initialization steps for your lab.
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
Task 1. Create a Cloud SQL PostgreSQL database
Open a new web browser window and navigate to the Google Cloud Console (console.cloud.google.com). Use the project selector to choose
the first project with a leading name of 'qwiklabs-gcp.'
On the Navigation menu (), click SQL.
If SQL is not listed in the navigation menu, click View All Products. In the All Products page, scroll to the section on Databases , and then click SQL.
Click Create Instance and choose the PostgreSQL option.
Under Choose a Cloud SQL edition select Enterprise.
Name the instance ID postgresql-db and enter a password you can remember (or if you prefer, click the Generate button and copy the generated password to a text file).
For Choose region and zonal availability set the region to and select Multiple zones.
Accept the rest of the defaults and click Create Instance.
Note: It will take 5-10 minutes for your database to be ready.
When the database is ready, click its name in the console to see its details.
Click Check my progress to verify the objective.
Create a Cloud SQL PostgreSQL instance
Task 2. Connect to the database using the Cloud SDK
In the Connect to this instance section, click the Open Cloud Shell link.
This opens Cloud Shell at the bottom of the window.
If prompted, click Continue.
Notice that the connection command is already entered at the prompt.
Press ENTER to run the command and then click Authorize if prompted.
It will take a little while for Cloud Shell's IP address to be allowlisted by the Cloud SQL network.
When prompted, enter your password.
To see the databases, type the following (a backslash and the lowercase letter L):
\l
To create a database, enter the following command:
CREATE DATABASE petsdb;
Confirm that it worked:
\l
To connect to the database, enter the following command, and then enter your password when prompted:
\c petsdb;
Create a table:
CREATE TABLE Pets(ID INT PRIMARY KEY NOT NULL, Name TEXT, Breed TEXT);
Add a record:
INSERT INTO Pets (ID, Name, Breed) Values (1, 'Noir', 'Schnoodle');
Run a query:
SELECT * FROM Pets;
To disconnect from the database, press CTRL+C and return to Cloud Shell.
Click Check my progress to verify the objective.
Create a database on Cloud SQL PostgreSQL instance
Task 3. Create a Cloud SQL MySQL database using the CLI
At the Cloud Shell command prompt, enter the following command to create a MySQL database using the CLI:
It will take a few minutes for the database to be ready.
When the command completes, make note of the database server's primary IP address. Copy and paste this address in a text file, because you will need it later.
Enter the following command to set the password for the root account. Make the password something you can remember.
Enter the following command to connect to your Cloud SQL database using the CLI. It will take a little while for Cloud Shell's IP address to be allowlisted by the Cloud SQL network.
gcloud sql connect mysql-db --user=root --quiet
When prompted, enter your password.
At the mysql prompt, type the following to ensure that it works:
SHOW DATABASES;
To return to the Cloud Shell command prompt, type exit.
Click Check my progress to verify the objective.
Create a Cloud SQL MySQL database using the CLI
Task 4. Connect to the MySQL database from a virtual machine
In Cloud Shell, enter the following command to create a Debian 11 virtual machine that you can use as a test client:
When the command completes, copy the external IP address to the clipboard. You use this IP address to allow this machine to access to the database.
Return to the Console and the Cloud SQL service, and then select the mysql-db database to see its details.
Click Edit from the Overview list at the top of the page.
Open the Connections section, and then click Add Network under Authorized networks. Name the new connection VM and paste the virtual machine's external IP address in the Network text box.
Click Done, and then click Save.
Once the update is complete, return to the Cloud Shell, enter the following command to SSH into the test client:
To log on to the database server, use the command below but change the IP address to your Cloud SQL database's IP address. (You should have pasted this address in a text file earlier in the lab. If not, you can find it in the Cloud SQL console.)
mysql --host=[Database Public IP Address] --user=root --password
At the mysql prompt, type the following to ensure that it works:
SHOW DATABASES;
To return the the Cloud Shell command prompt, type exit.
Click Check my progress to verify the objective.
Connect to the MySQL database from a virtual machine
Congratulations! You used Cloud SQL to create databases that are managed by Google. You created both PostgreSQL and MySQL databases, and you connected to those databases using the Google Cloud CLI and from a virtual machine.
End your lab
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
1 star = Very dissatisfied
2 stars = Dissatisfied
3 stars = Neutral
4 stars = Satisfied
5 stars = Very satisfied
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
Lab membuat project dan resource Google Cloud untuk jangka waktu tertentu
Lab memiliki batas waktu dan tidak memiliki fitur jeda. Jika lab diakhiri, Anda harus memulainya lagi dari awal.
Di kiri atas layar, klik Start lab untuk memulai
Gunakan penjelajahan rahasia
Salin Nama Pengguna dan Sandi yang diberikan untuk lab tersebut
Klik Open console dalam mode pribadi
Login ke Konsol
Login menggunakan kredensial lab Anda. Menggunakan kredensial lain mungkin menyebabkan error atau dikenai biaya.
Setujui persyaratan, dan lewati halaman resource pemulihan
Jangan klik End lab kecuali jika Anda sudah menyelesaikan lab atau ingin mengulanginya, karena tindakan ini akan menghapus pekerjaan Anda dan menghapus project
Konten ini tidak tersedia untuk saat ini
Kami akan memberi tahu Anda melalui email saat konten tersedia
Bagus!
Kami akan menghubungi Anda melalui email saat konten tersedia
Satu lab dalam satu waktu
Konfirmasi untuk mengakhiri semua lab yang ada dan memulai lab ini
Gunakan penjelajahan rahasia untuk menjalankan lab
Gunakan jendela Samaran atau browser pribadi untuk menjalankan lab ini. Langkah ini akan mencegah konflik antara akun pribadi Anda dan akun Siswa yang dapat menyebabkan tagihan ekstra pada akun pribadi Anda.
In this lab, you will use Google Cloud SQL to create databases that are managed by Google. You will create both PostgreSQL and MySQL databases and you will connect to those databases using the Google Cloud CLI and from a virtual machine.