mysqldump Command Tutorial



The mysqldump command is a powerful utility provided by MySQL that allows you to create backups of your databases. It generates a SQL script that contains all the commands needed to recreate the database from scratch.

This tutorial will explore the mysqldump command in detail, including its installation, configuration, and usage with practical examples.

Table of Contents

Here is a comprehensive guide to the options available with the mysqldump command −

How to Use mysqldump Command in Linux?

Before we dive into the mysqldump command, ensure you have the following −

Step 1: Install MySQL

If you haven't installed MySQL yet, you can do so using the following commands −

sudo apt update
sudo apt install mysql-server -y
mysqldump Command in Linux1

To create a backup of a MySQL database, use the following command −

mysqldump -u [username] -p [database_name] > [backup_file.sql]

Replace [username] with your MySQL username, [database_name] with the name of the database you want to back up, and [backup_file.sql] with the name of the backup file.

Step 4: Creating a Backup of All Databases

mysqldump -u [username] -p --all-databases > [backup_file.sql]

This command generates a SQL script that contains the backup of all databases on the server.

Step 5: Creating a Backup of Specific Tables

mysqldump -u [username] -p [database_name] [table1] [table2] > [backup_file.sql]

Replace [table1] and [table2] with the names of the tables you want to back up.

Step 6: Excluding Specific Tables from the Backup

To exclude specific tables from the backup, use the --ignore-table option:

mysqldump -u [username] -p [database_name] --ignore-table=[database_name].[table1] --ignore-table=[database_name].[table2] > [backup_file.sql]

Replace [table1] and [table2] with the names of the tables you want to exclude from the backup.

Step 7: Compressing the Backup File

To compress the backup file, you can pipe the output of mysqldump to a compression utility like gzip:

mysqldump -u [username] -p [database_name] | gzip > [backup_file.sql.gz]

This command creates a compressed backup file with the .gz extension.

Examples of mysqldump Command in Linux

Let's explore some detailed examples and explanations of the mysqldump command and its usage.

Creating a Backup of a Single Database

To create a backup of a single database named mydatabase, use the following command:

mysqldump -u root -p mydatabase > mydatabase_backup.sql
mysqldump Command in Linux2

This command prompts you for the MySQL root password and creates a backup file named mydatabase_backup.sql.

Creating a Backup of All Databases

To create a backup of all databases on the MySQL server, use the --all-databases option:

mysqldump -u root -p --all-databases > all_databases_backup.sql
mysqldump Command in Linux3

This command prompts you for the MySQL root password and creates a backup file named all_databases_backup.sql containing the backup of all databases.

Creating a Backup of Specific Tables

To create a backup of specific tables named table1 and table2 within a database named mydatabase, use the following command:

sudo mysqldump -u root -p mydatabase table1 table2 > specific_tables_backup.sql
mysqldump Command in Linux4

This command prompts you for the MySQL root password and creates a backup file named specific_tables_backup.sql containing the backup of the specified tables.

Excluding Specific Tables from the Backup

To exclude specific tables named table1 and table2 from the backup of a database named mydatabase, use the --ignore-table option:

sudo mysqldump -u root -p mydatabase --ignore-table=mydatabase.table1 --ignore-table=mydatabase.table2 > exclude_tables_backup.sql
mysqldump Command in Linux5

This command prompts you for the MySQL root password and creates a backup file named exclude_tables_backup.sql excluding the specified tables.

Compressing the Backup File

To compress the backup file using gzip, pipe the output of mysqldump to gzip:

sudo mysqldump -u root -p mydatabase | gzip > mydatabase_backup.sql.gz
mysqldump Command in Linux6

This command prompts you for the MySQL root password and creates a compressed backup file named mydatabase_backup.sql.gz.

Restoring a Backup

To restore a backup created with mysqldump, use the mysql command:

sudo mysql -u root -p mydatabase < mydatabase_backup.sql
mysqldump Command in Linux7

This command prompts you for the MySQL root password and restores the database from the backup file named mydatabase_backup.sql.

Advanced Usage and Options

The mysqldump command supports various advanced options that provide additional functionality and flexibility. Here are some commonly used options −

--add-drop-database: Adds a DROP DATABASE statement before each CREATE DATABASE statement in the backup file.

sudo mysqldump -u root -p --add-drop-database mydatabase > mydatabase_backup.sql
mysqldump Command in Linux8

--add-drop-table: Adds a DROP TABLE statement before each CREATE TABLE statement in the backup file

sudo mysqldump -u root -p --add-drop-table mydatabase > mydatabase_backup.sql
mysqldump Command in Linux9

--add-locks: Adds LOCK TABLES and UNLOCK TABLES statements around each table dump to ensure consistency.

sudo mysqldump -u root -p --add-locks mydatabase > mydatabase_backup.sql
mysqldump Command in Linux10

--single-transaction: Creates a consistent snapshot of the database by using a single transaction. This option is useful for InnoDB tables.

sudo mysqldump -u root -p --single-transaction mydatabase > mydatabase_backup.sql
mysqldump Command in Linux11

--routines: Includes stored routines (procedures and functions) in the backup file.

sudo mysqldump -u root -p --routines mydatabase > mydatabase_backup.sql
mysqldump Command in Linux12

--triggers: Includes triggers in the backup file.

code sudo mysqldump -u root -p --triggers mydatabase > mydatabase_backup.sql
mysqldump Command in Linux13

--events: Includes events in the backup file.

sudo mysqldump -u root -p --events mydatabase > mydatabase_backup.sql
mysqldump Command in Linux14

--hex-blob: Dumps binary columns using hexadecimal notation.

sudo mysqldump -u root -p --hex-blob mydatabase > mydatabase_backup.sql
mysqldump Command in Linux15

Troubleshooting Tips for mysqldump Command

Here are some common issues and troubleshooting tips for using mysqldump:

Permission Issues: Ensure that the MySQL user has the necessary permissions to access the database and create backups. Use the GRANT statement to assign the required privileges:

GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER ON mydatabase.* TO 'username'@'localhost';

Large Databases: For large databases, consider using the --single-transaction option to create a consistent snapshot without locking the tables. Additionally, use compression to reduce the size of the backup file.

Character Set Issues: Ensure that the character set used by mysqldump matches the character set of the database. Use the --default-character-set option to specify the character set:

sudo mysqldump -u root -p --default-character-set=utf8 mydatabase > mydatabase_backup.sql
mysqldump Command in Linux16

Conclusion

The mysqldump command is used to create a logical backup of a MySQL database. It generates a SQL script that contains all the SQL statements needed to recreate the database, including table structures, data, and indexes. This script can be used to restore the database on the same or a different MySQL server.

Advertisements