Backup and Restore a MySQL Database Using mysqldump
📖 Scenario: You are managing a small business database. To keep your data safe, you want to create a backup of your MySQL database and later restore it if needed.
🎯 Goal: Learn how to use the mysqldump command to export a database to a file and then import it back to restore the database.
📋 What You'll Learn
Create a MySQL database named shop with a table products and some sample data.
Use mysqldump to export the shop database to a file named shop_backup.sql.
Create a new empty database named shop_restore.
Use mysqldump output file to restore data into shop_restore.
💡 Why This Matters
🌍 Real World
Database backups are essential to protect data from loss due to errors, crashes, or attacks. Using mysqldump is a common way to create logical backups of MySQL databases.
💼 Career
Database administrators and developers often need to backup and restore databases safely. Knowing mysqldump commands is a fundamental skill for managing MySQL databases.
Progress0 / 4 steps
1
DATA SETUP: Create the shop database and products table with sample data
Create a MySQL database called shop. Then create a table called products with columns id (integer primary key), name (varchar 50), and price (decimal). Insert these exact rows into products: (1, 'Pen', 1.20), (2, 'Notebook', 2.50), (3, 'Eraser', 0.50).
MySQL
Hint
Use CREATE DATABASE, CREATE TABLE, and INSERT INTO statements with exact names and values.
2
CONFIGURATION: Prepare to export the shop database using mysqldump
Write the exact mysqldump command to export the shop database to a file named shop_backup.sql. Assume the MySQL username is root and no password is needed.
MySQL
Hint
Use mysqldump -u root shop > shop_backup.sql to export the database.
3
CORE LOGIC: Create a new empty database shop_restore to prepare for restoring
Create a new MySQL database called shop_restore which will be used to restore the backup data.
MySQL
Hint
Use CREATE DATABASE shop_restore; to create the new database.
4
COMPLETION: Restore the shop_backup.sql file into the shop_restore database
Write the exact command to import the shop_backup.sql file into the shop_restore database using MySQL command line. Use username root and no password.
MySQL
Hint
Use mysql -u root shop_restore < shop_backup.sql to restore the backup.
Practice
(1/5)
1. What is the primary purpose of the mysqldump command in MySQL?
easy
A. To create a backup file of a database
B. To delete a database permanently
C. To optimize database performance
D. To create new tables in a database
Solution
Step 1: Understand the function of mysqldump
mysqldump is a utility used to export database contents into a file for backup or transfer.
Step 2: Identify the correct purpose
It does not delete or optimize databases; it creates a backup file.
Final Answer:
To create a backup file of a database -> Option A
Quick Check:
mysqldump = backup creation [OK]
Hint: Remember mysqldump = dump database to file [OK]
Common Mistakes:
Confusing mysqldump with delete commands
Thinking it optimizes database
Assuming it creates tables
2. Which of the following is the correct syntax to back up a database named shopdb using mysqldump?
easy
A. mysqldump backup.sql > shopdb -u root -p
B. mysqldump shopdb -u root -p backup.sql
C. mysqldump -p -u root backup.sql shopdb
D. mysqldump -u root -p shopdb > backup.sql
Solution
Step 1: Recall mysqldump syntax
The correct syntax is mysqldump -u [user] -p [database_name] > [file].
Step 2: Match the syntax with options
mysqldump -u root -p shopdb > backup.sql matches the correct order and redirection to a file.
Final Answer:
mysqldump -u root -p shopdb > backup.sql -> Option D