How to export your MySQL database using mysqldump

Use your command line to backup your mysql database by exporting it to an .sql file. All you need to complete this task is shell access & a mysql account.

A major use for this command is to easily migrate your sites from one server to another. Since this is often part of a larger process, I like to decide ahead of time which directory will contain this file. So I’ll start by navigating to my desired directory and scoping it out:

user@server:~$ cd /path/to/dir/; ls -al

Now that you’re sure about using this directory. Here is the basic mysqldump command:

user@server:/path/to/dir$ mysqldump db_name > db_backup.sql

But, depending on your level of access, you may need to modify it:

user@server:/path/to/dir$ mysqldump -u mysql_user -p db_name > db_backup.sql
Enter password:

After entering your mysql password, a file called db_backup.sql will be created in the default location, which is your current directory (three cheers for planning ahead!).
Of course, we could have just as easily added a location statement within the command:

user@server:~$ mysqldump -u mysql_user -p db_name > /path/to/dir/db_backup.sql

But, you’re going to want to cd there anyways, just to verify that everything’s in order 🙂

Be sure to browse our related posts describing how to get migrate files from one server to another, and how to import an .sql file into an empty MySQL databse.

Leave a Comment