mysqldump is an effective tool to backup MySQL database. It creates a *.sql file withDROP table, CREATE table and INSERT intosql-statements of the source database. To restore the database, execute the *.sql file on destination database. For MyISAM, use mysqlhotcopy method that we explained earlier, as it is faster for MyISAM tables.
Using mysqldump, you can backup a local database and restore it on a remote database at the same time, using a single command. In this article, let us review several practical examples on how to use mysqldump to backup and restore.
Using mysqldump, you can backup a local database and restore it on a remote database at the same time, using a single command. In this article, let us review several practical examples on how to use mysqldump to backup and restore.
For the impatient, here is the quick snippet of how backup and restore MySQL database using mysqldump:
backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql restore:# mysql -u root -p[root_password] [database_name] < dumpfilename.sql
How To Backup MySQL database
1. Backup a single database:
This example takes a backup of sugarcrm database and dumps the output to sugarcrm.sql
# mysqldump -u root -ptmppassword sugarcrm > sugarcrm.sql # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
2. Backup multiple databases:
If you want to backup multiple databases, first identify the databases that you want to backup using the show databases as shown below:
# mysql -u root -ptmppassword mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bugs | | mysql | | sugarcr | +--------------------+ 4 rows in set (0.00 sec)
For example, if you want to take backup of both sugarcrm and bugs database, execute the mysqldump as shown below:
# mysqldump -u root -ptmppassword --databases bugs sugarcrm > bugs_sugarcrm.sql
Verify the bugs_sugarcrm.sql dumpfile contains both the database backup.
# grep -i "Current database:" /tmp/bugs_sugarcrm.sql -- Current Database: `mysql` -- Current Database: `sugarcrm`
3. Backup all the databases:
The following example takes a backup of all the database of the MySQL instance.
# mysqldump -u root -ptmppassword --all-databases > /tmp/all-database.sql
How To Restore MySQL database
1. Restore a database
In this example, to restore the sugarcrm database, execute mysql with < as shown below. When you are restoring the dumpfilename.sql on a remote database, make sure to create the sugarcrm database before you can perform the restore.# mysql -u root -ptmppassword mysql> create database sugarcrm; Query OK, 1 row affected (0.02 sec) # mysql -u root -ptmppassword sugarcrm < /tmp/sugarcrm.sql # mysql -u root -p[root_password] [database_name] < dumpfilename.sql
2. Backup a local database and restore to remote server using single command:
This is a sleek option, if you want to keep a read-only database on the remote-server, which is a copy of the master database on local-server. The example below will backup the sugarcrm database on the local-server and restore it as sugarcrm1 database on the remote-server. Please note that you should first create the sugarcrm1 database on the remote-server before executing the following command.[local-server]# mysqldump -u root -ptmppassword sugarcrm | mysql \ -u root -ptmppassword --host=remote-server -C sugarcrm1 [Note: There are two -- (hyphen) in front of host]
No comments:
Post a Comment