How To Backup & Restore MySQL database using mysqldump

mysqldump tool provide facility to backup MySQL databases. It creates *.sql file with DROP, CREATE and INSERT statements. There are number of keys, which can be used to create backup of database(s).

Details of all keys can be checked using command
bash: # mysqldump --help

Basic command for taking backup

bash: # mysqldump -u root -p[root_password] [database_name] > filename.sql

And for restoring

bash: # mysql -u root -p[root_password] [database_name] < filename.sql

Backup a single database

bash: # mysqldump -u root -ptmppassword userdb > userdb.sql
bash: # mysqldump -u root -p[root_password] [database_name] > filename.sql

Backup multiple databases

bash: # mysql -u root -ptmppassword
mysql> show databases;

After selection of databases execute following command on bash command prompt
bash: # mysqldump -u root -ptmppassword --databases db1 db2 > filename.sql

Backup all the databases

bash: # mysqldump -u root -ptmppassword --all-databases > /path/to_filename.sql

Backup a specific table(s)

bash: # mysqldump -u root -ptmppassword db1 table2 > /path/filename.sql

How To Restore MySQL database using mysql bash command

bash: # mysql -u root -ptmppassword
mysql> create database db1;
bash: # mysql -u root -ptmppassword db1 < /path to/filename.sql
bash: # mysql -u root -p[root_password] [database_name] < sqlfilename.sql

Backup a local database & restore to remote server with single command

bash[localServer]: # mysqldump -u root -ptmppassword db1 | mysql
-u root -ptmppassword --host=remote-server -C db1

Citation: online resources on mysql.com

Leave a Reply

Your email address will not be published. Required fields are marked *


8 − = two