Labs Restore Data Base
Labs Restore Data Base
In this lab, you will learn how to use the MySQL command line interface (CLI) to create different types of backups of a database and restore the structure and data of
a database with your created backups when needed.
Objectives
After completing this lab, you will be able to use the MySQL command line to:
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 World database:
The first row is the table name, the second is the primary key, and the remaining items are any additional attributes.
Exercises
This lab is divided into two exercises: an Example Exercise and Practice Exercise.
about:blank 1/5
16/11/2024 08:51 about:blank
A logical backup creates a file containing DDL (such as create table) and DML commands (such as insert) that recreate the objects and data in the database. As such,
you can use this file to recreate the database on the same or on another system. Generally, when you perform a logical backup and restore, you reclaim any wasted
space from the original database since the restoration process creates a clean version of the tables. Logical backups enable you to backup granular objects. For
example, you can back up an individual database table, however, you cannot use it to backup log files or database configuration settings. Suppose you are in a
situation where you dropped one or more tables of a database accidentally. This is where you make use of the logical backup of a database table to restore the
structure and data of the table.
1. Start the MySQL service session using the Start MySQL in IDE button directive.
NOTE: Whenever you are required to enter your MySQL service session password from the MySQL service session tab at any step of the lab, copy the password
displayed under the Connection Information section when MySQL started up. Paste the password into the terminal using Ctrl + V (Mac: ⌘ + V), and press Enter
on the keyboard. For security reasons, you will not see the password as it is entered on the terminal.
3. Click New Terminal button from the mysql service session tab. Now you need to fetch two mysql script files to the Cloud IDE user session storage. Copy the
command below by clicking on the little copy button on the bottom right of the codeblock. Then paste it into the terminal at the command line prompt using
Ctrl + V (Mac: ⌘ + V), and Enter on keyboard. Do this for each of the commands below one at a time.
world_mysql_script.sql
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0231EN-SkillsNetwork/datasets/World/world_my
world_mysql_update_A.sql
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0231EN-SkillsNetwork/datasets/World/world_my
4. Initiate a mysql command prompt session by clicking the MySQL CLI button from the mysql service session tab.
about:blank 2/5
16/11/2024 08:51 about:blank
5. Create a new database world using the command below in the terminal:
CREATE DATABASE world;
6. To use the newly created world database, use the command below in the terminal:
USE world;
7. Execute the world mysql script (world_mysql.sql) to complete the world database creation process using the command below in the terminal:
SOURCE world_mysql_script.sql;
8. To list all the table names from the world database, use the command below in the terminal:
SHOW TABLES;
9. Retrieve all the Canada (countrycode=’CAN’) related records from the countrylanguage table using the command below in the terminal:
about:blank 3/5
16/11/2024 08:51 about:blank
SELECT * FROM countrylanguage WHERE countrycode='CAN';
10. You will observe the returned result set is empty set. This means Canada related records are currently absent from the table. Run the update script
(world_mysql_update_A.sql) to insert the records you were looking for.
SOURCE world_mysql_update_A.sql;
12. Quit the MySQL command prompt session using the command below in the terminal:
\q
13. Now backup the countrylanguage table of the world database using the command below in the terminal (enter your MySQL service session password from
the MySQL service session tab if necessary):
mysqldump --host=mysql --port=3306 --user=root --password world countrylanguage > world_countrylanguage_mysql_backup.sql
14. To view the contents of the backup file within the terminal, use the command below:
cat world_countrylanguage_mysql_backup.sql
15. Run the command below in the terminal (enter your MySQL service session password from the MySQL service session tab if necessary):
16. To list all the table names from the world database, use the command below in the terminal (enter your MySQL service session password from the MySQL
service session tab if necessary):
mysql --host=mysql --port=3306 --user=root --password --execute="SHOW TABLES FROM world;"
about:blank 4/5
16/11/2024 08:51 about:blank
17. You will observe the table countrylanguage is missing from the world database. Now you are in the situation where you dropped a table of a database
accidentally. This is where you will make use of the backup of the database table (you created backup world_countrylanguage_mysql_backup.sql) to restore
the structure and data of the table.
18. To restore the structure and data of the table countrylanguage, use the command below in the terminal (enter your MySQL service session password from the
MySQL service session tab if necessary):
mysql --host=mysql --port=3306 --user=root --password world < world_countrylanguage_mysql_backup.sql
20. Again retrieve all the Canada (countrycode=’CAN’) related records from the countrylanguage table using the command below in the terminal (enter your
MySQL service session password from the MySQL service session tab if necessary):
mysql --host=mysql --port=3306 --user=root --password --execute="SELECT * FROM world.countrylanguage WHERE countrycode='CAN';"
Scenario: You are planning to update and migrate one of the tables from your world database to a new MySQL server. Perform a logical backup of the
table city from the database world. The backup table is expected to contain data of Bangladesh. Validate if your created backup is in working state.
Congratulations! You have completed this lab, and you are ready for the next topic.
Author(s)
Sandip Saha Joy
Other Contributor(s)
David Pasternak
about:blank 5/5