Getting Started With MySQL Command Line
Getting Started With MySQL Command Line
appdo…
+
-
Helvetica N...
▼
Step 1 of 10
In this lab, you will use the MySQL command line interface (CLI) to create a database and to restore the structure and contents of tables. Then you will learn
how to explore and query tables. Finally, you will learn how to dump/backup tables from database.
To complete this lab you will utilize the MySQL relational database service available as part of the IBM Skills Network Labs (SN Labs) Cloud IDE. SN Labs is
a virtual lab environment used in this course.
You will use a modified version of the database for the lab, so to follow the lab instructions successfully please use the database provided with the lab, rather
than the database from the original source.
The following ERD diagram shows the schema of the Sakila database:
https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdomain.cloud%2FIBM-D… 1/8
09/10/2021, 15:02 https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdo…
Objectives
After completing this lab, you will be able to use the MySQL command line to:
Create a database.
Restore the structure and data of a table.
Explore and query tables.
Dump/backup tables from a database.
Exercise
In this exercise through different tasks, you will use the MySQL command line interface (CLI) to create a database and to restore the structure and contents of
tables. Then you will learn how to explore and query tables. Finally, you will learn how to dump/backup tables from database.
2. Copy the command below by clicking on the little copy button on the bottom right of the codeblock and then paste it into the terminal using Ctrl + V
(Mac: ⌘ + V) to fetch the sakilamysqldump.sql file to the Cloud IDE.
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0110EN-SkillsNetwork/datasets/sakila/sakila_mysql_dum
https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdomain.cloud%2FIBM-D… 2/8
09/10/2021, 15:02 https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdo…
3. Start the MySQL service session in the Cloud IDE using the command below in the terminal:
start_mysql
4. Initiate the mysql command prompt session within MySQL service session using the command below in the terminal:
https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdomain.cloud%2FIBM-D… 3/8
09/10/2021, 15:02 https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdo…
5. Enter your MySQL service session password from the highlighted location of the terminal shown in the image above. Note down your MySQL service
session password because you may need to use it later in the lab.
6. Create a new database sakila using the command below in the terminal and proceed to Task B:
create database sakila;
1. To use the newly created empty sakila database, use the command below in the terminal:
use sakila;
2. Restore the sakila mysql dump file (containing the sakila database table definitions and data) to the newly created empty sakila database using the
command below in the terminal and proceed to Task C:
source sakila_mysql_dump.sql;
Note: You can use the source command to restore the database dump file within the mysql command prompt. To restore the database dump
file outside of the mysql command prompt, you can use the mysql --host=127.0.0.1 --port=3306 --user=root --password sakila <
sakila_mysql_dump.sql command after quitting the mysql command prompt session with command \q.
https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdomain.cloud%2FIBM-D… 4/8
09/10/2021, 15:02 https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdo…
2. Explore the structure of the staff table using the command below in the terminal:
DESCRIBE staff;
3. Now retrieve all the records from the staff table using the command below in the terminal:
https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdomain.cloud%2FIBM-D… 5/8
09/10/2021, 15:02 https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdo…
4. Quit the MySQL command prompt session using the command below in the terminal and proceed to Task D:
\q
1. Finally, dump/backup the staff table from the database using the command below in the terminal:
mysqldump --host=127.0.0.1 --port=3306 --user=root --password sakila staff > sakila_staff_mysql_dump.sql
3. To view the contents of the dump file within the terminal, use the command below:
cat sakila_staff_mysql_dump.sql
https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdomain.cloud%2FIBM-D… 6/8
09/10/2021, 15:02 https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdo…
Congratulations! You have completed this lab, and you are ready for the next topic.
Author(s)
Sandip Saha Joy
Other Contributor(s)
*
Changelog
https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdomain.cloud%2FIBM-D… 7/8
09/10/2021, 15:02 https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdo…
Date Version Changed by Change Description
2021-03-15 1.0 Sandip Saha Joy Created initial version
Continue
https://labs.cognitiveclass.ai/tools/theiadocker/?md_instructions_url=https%3A%2F%2Fcf-courses-data.s3.us.cloud-object-storage.appdomain.cloud%2FIBM-D… 8/8