Sometimes you need to dump a MySQL database so it can be restored on the same server or moved somewhere else. mysqldump is one of the quickest ways to do that from the command line.

Backup

1
mysqldump -uuser -ppassword database_name > database_name.sql

Replace user, password, and database_name with your own values.

One important detail: do not add spaces after -u or -p.

Restore

1
mysql -uuser -ppassword database_name < database_name.sql

This restores the SQL dump back into the target database.

It is a simple workflow, but it is still one of the most useful command-line habits to know when working with MySQL.