mysqldump Command: Tutorial & Examples

Creating a backup of your database

The mysqldump command allows you to create backups of one or more MySQL databases. It generates a SQL script that can be used to recreate the database at any point in time. This is particularly useful for backing up your database or migrating data from one server to another.

How Does mysqldump Work?

mysqldump works by generating a .sql file with SQL commands that can recreate the database from scratch. The command connects to the MySQL server, retrieves the required information and formats it in SQL.

Having regular backups is an important part of maintaining any kind of data, and databases are no exception. The mysqldump command makes it easy to create backups of your MySQL databases, which can be a lifesaver in case of a data loss incident.

How to use mysqldump

To use mysqldump, you need to have sufficient privileges to access the database you want to dump. The basic syntax of the mysqldump command is as follows:

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

Replace [username], [database_name], and [dump_file.sql] with your MySQL username, the name of the database you want to dump, and the name of the dump file you want to create, respectively.

For example:

mysqldump -u root -p my_database > my_database.sql

After running this command, you will be prompted to enter the password for the MySQL user.

Common Command Line Parameters

Here are some common parameters that you can use with the mysqldump command:

  • --all-databases or -A: Dumps all databases.
  • --no-data or -d: Dumps only the database structure, not the data.
  • --add-drop-table: Adds a DROP TABLE statement before each CREATE TABLE in the dump.

For example, to dump all databases:

mysqldump -u root -p --all-databases > all_databases.sql

These are other important mysqldump command line options that allow you to customize your backup:

  • --quick: This option forces mysqldump to retrieve rows for a table from the server a row at a time rather than retrieving the entire row set and buffering it in memory before writing it out.
  • --compact: This option enables a more compact output.
  • --insert-ignore: Add the IGNORE keyword to INSERT statements.
  • --no-create-info: Do not write CREATE TABLE statements that re-create each dumped table.
  • --skip-lock-tables: Do not lock the tables before dumping them.
  • --where: Dump only rows selected by the given WHERE condition.

Better Backups Using Pipes

It's very easy to combine mysqldump with other commands through the use of pipes. For example, you can create a compressed backup like this:

mysqldump --quick --skip-lock-tables -u user mydatabase mytable | zstd -c > backup.sql.zst

This uses the zstd command to compress the data on the fly while writing to disk.

Another handy option is to use mysqldump to directly sync data between two servers. This is done by executing mysqldump on a remote server using ssh and piping the data to another server:

$start="2022-01-01 00:00:00"
$end="2023-01-01 00:00:00"
ssh server1 "mysqldump --quick --skip-lock-tables --insert-ignore --no-create-info --compact --where \"modify_time >= '$start' and modify_time < '$end'\" mydatabase mytable" | ssh server2 "mysql mydatabase"

This command will copy all data of the year 2022 in mytable from server1 to server2 inserting only rows that do not yet exist.

Potential Problems and Pitfalls

While mysqldump is a powerful tool, it's not without its potential issues. For example, if you're dealing with large databases, mysqldump can take a long time to complete and consume a lot of system resources. This is where the --quick option can help.

One common issue with mysqldump is that it locks the tables during the dump process. This means that if your application is writing to the database while the dump is happening, it might be locked out and unable to write. This can cause problems in a production environment. In this case, your can use --skip-lock-tables, but be aware that this may also cause problems, when new data is being written while the dump is in progress.

Also, the dump file can be quite large, especially for big databases. Make sure you have enough disk space before starting the dump process. If you run out of disk space, the dump process will fail, potentially leaving you with a partial and unusable dump file.

Finally, remember that using mysqldump requires certain privileges. If your user doesn't have these privileges, you won't be able to perform the backup.

Conclusion

The mysqldump command is a powerful tool for any system administrator working with MySQL databases. It allows you to easily create backups of your databases, which is essential for data safety and integrity. As with any powerful tool, it comes with its own set of potential issues, but with understanding and careful usage, it can be an invaluable part of your Linux toolkit.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA