How to backup MySQL database from the command line

Backup your MySQL database to an sql file with your command line. Using native mysql client and tools you can perform all the backup and restore operations you need.

First you need to have installed the command line tools, in this case mysqldump.

$ mysqldump --opt -u [uname] -p [dbname] > [backupfile.sql]
  • [uname] Your database username
  • [pass] The password for your database (note there is no space between -p and the password)
  • [dbname] The name of your database
  • [backupfile.sql] The filename for your database backup
  • [–opt] The mysqldump option

Example if you have a database called wordpress_blog you only have to do this:

$ mysqldump --opt -u root -p wordpress_blog > wordpress_blog_bk.sql

If you want to backup all the databases in only one sql file you can use –all-databases option

$ mysqldump -u root -p --all-databases > alldb_backup.sql