mysqldump by example

MySQL Dump is probably one of the best tool to take copies of databases, and it comes with MySQL, so you don’t need to install more stuff.

Example 1: Backup all databases (Use with caution, see below)
IMPORTANT: if you dump this to another server, you will lose all users on the target server, this is because the database named mysql (not the database engine but the actual database that has the users) on the target server is overwritten by the one from the source

Added note: If you want to monitor how large the uncompressed dump file has gone, or in other words, how much data mysqldump has brought so far, you can use PV, in this example, i expect the data to be 123GBs so i put that in so that PV can tell me what percentage of that i have finished, it will tell me the exact number of bytes anyways, but this is visually easier.

mysqldump --opt -u root --password="yourpass" databasename | pv -s 123g | pigz -c > dumpfile.sql.gz


1- Dump all databases

mysqldump -u root --password="thispassword" --all-databases > thisdatabasedump.sql

2- Dump all databases and gzip compress the output file, gzcompress is like compression used in zip files, this command compresses on the fly (make sure gzip is installed)

mysqldump --opt -u root --password="thispassword" --all-databases | gzip -9 > thisdatabasedump.sql.gz

3- Dump all databases and BZIP compress the output file, BZIP compression is better than gzip compression, but takes significantly more time, like the one before this command compresses on the fly (make sure you have bzip2 installed)

mysqldump --opt -u root --password="thispassword" --all-databases | bzip2 > thisdatabasedump.sql.bz2

4- If you have a server with multiple processors, you can overcome the slowness of bzip2 by simply making all the CPUs (real or virtual or hyper threaded) work on compressing at the same time, the application is called parallel bzip2 (make sure pbzip2 is installed)

mysqldump --opt -u root --password="thispassword" --all-databases | pbzip2 > thisdatabasedump.sql.bz2

4.5- If your server has 8CPUs and you only want 7 of them to do zipping so that one of them can be dedicated to mysqldump

mysqldump --opt -u root --password="thispassword" --all-databases | pbzip2 -p7 > thisdatabasedump.sql.bz2

I will not give any more examples about compression, obviously, as you can see from the examples above, to compress on the fly all you need to do is replace the section ( > thisdumpfile.sql ) with ( | pbzip2 > thisdumpfile.sql.bz2 )

5- Dump a certain database to the file

mysqldump --opt -u root --password="thispassword" thisparticulardbsname > thisdumpfile.sql

6- Dump certain databaseS

mysqldump --opt -u root --password="thispassword" --databases db1name db2name db3name db4name > thisdumpfile.sql

7- Dump certain tables from within a database

mysqldump --opt -u root --password="thispassword" databasename table1name table2name table3name > thisdumpfile.sql

8- Exclude certain tables from the mysqldump

 mysqldump --opt -u username --password="thispassword" databasename --ignore-table=databasename.table1 --ignore-table=databasename.table2 > database.sql

Leave a Reply

Your email address will not be published. Required fields are marked *