Exporting Specific Tables in MySQL Using mysqldump

The other day we needed to restore a single table from a database for our testing environment. Thankfully, MySQL provides an easy solution to this problem.

One of the command line tools that come with MySQL is mysqldump which is used to generate a dump file of a database. It outputs a series of SQL statements that can then be used to restore the database.

In the most basic form, it’s run like the following.

mysqldump [database_name] > whole.sql

This will create a file named “whole.sql” that will contain the entire contents of the “database_name” database.

Then when we need to restore the database we can use the mysql function.

mysql [database_name] < whole.sql

If we want to export a specific table we can specify the table names as the last arguments to the mysqldump function.

mysqldump [database_name] [table_name1 table_name2 ...] > partial.sql

This will only export table_name1 and table_name2 and it can be restored the same way.

mysql [database_name] < partial.sql