Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
159 views

Getting Started With MySQL Command Line

This document provides instructions for using the MySQL command line interface to create a database, restore table structures and data, explore and query tables, and backup tables. The lab uses the Sakila sample database and teaches how to perform common MySQL administration tasks.

Uploaded by

Alaa Barazi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

Getting Started With MySQL Command Line

This document provides instructions for using the MySQL command line interface to create a database, restore table structures and data, explore and query tables, and backup tables. The lab uses the Sakila sample database and teaches how to perform common MySQL administration tasks.

Uploaded by

Alaa Barazi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 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…

+
-

Helvetica N...


Step 1 of 10

Hands-on Lab : Getting started with MySQL command line


Estimated time needed: 20 minutes

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.

Software Used in this Lab


In this lab, you will use MySQL. MySQL is a Relational Database Management System (RDBMS) designed to efficiently store, manipulate, and retrieve data.

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.

Database Used in this Lab


The Sakila database used in this lab comes from the following source: https://dev.mysql.com/doc/sakila/en/ under New BSD license [Copyright 2021 - Oracle
Corporation].

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.

Task A: Create a database


1. Go to Terminal > New Terminal to open a terminal from the side by side launched Cloud IDE.

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:

mysql --host=127.0.0.1 --port=3306 --user=root --password

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;

Task B: Restore the structure and data of a table

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.

Task C: Explore and query tables


1. To list all the tables names from the sakila database, use the command below in the terminal:
SHOW FULL TABLES WHERE table_type = 'BASE TABLE';

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:

SELECT * FROM staff;

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

Task D: Dump/backup tables from a database

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

2. Enter your MySQL service session password.

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

© IBM Corporation 2021. All rights reserved.

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

You might also like