How To Export And Import MySQL Databases Through The Linux Command Line

If you don't have access to a web-based tool like phpMyAdmin, you can still export and import MySQL databases. Here's how to do it through the Linux command line. (I usually use Ubuntu, but these instructions will probably work for your distribution too.)

The export command is mysqldump. It looks something like this:

mysqldump -u USER -p DATABASE > FILE.sql

Here's an example. Let's say we have a database user and a database both called wap. (We're backing up the Web App Path site.) Here's what you'd type into the command line:

mysqldump -u wap -p wap > wap.sql

It will prompt you for your password. Enter it, and MySQL will export your database to the wap.sql file.

When it's done, you will find your SQL file in the current working directory.

It's just as easy to import your database from the command line. The command is very similar:

mysql -u USER -p DATABASE < FILE.sql

Here's another example. We have the same database user and database called wap, as well as a SQL file called wap.sql. Here's what you'd type:

mysql -u wap -p wap < wap.sql

That's all there is to importing a database through the command line.