Monday 28 May 2018

How to Backup and Restore a MySQL Database


MySQL is a database server for storing data permanently. If you are using MySQL server for an production use, then its necessary to create database backup for recovering from any crash. MySQL provides an utility mysqldump for taking backups. In this article you will learn, to taken databases backup in .sql format for archive format. Also we will explain various options for it.
Options for Creating MySQL Databases Backup
You have many options for creating databases backups. read few options below. For this example we have using database name “mydb”.
1. Full Database Backup in Plain .sql File
 # mysqldump -u root -p mydb > mydb.sql
2. Full Database Backup in Archive .sql.gz File
 # mysqldump -u root -p mydb | gzip > mydb.sql.gz
3. Backup Single Table Only
 # mysqldump -u root -p mydb tbl_student > tbl_student.sql
4. Backup Multiple Databases
# mysqldump -u root -p --databases mydb1 mydb2 mydb3 > mydb1-mydb2-mydb3.sql
5. Backup All Databases
 # mysqldump -u root -p --all-databases > all-db-backup.sql
6. Backup Database Structure Only (no data)
 # mysqldump -u root -p --no-data mydb > mydb.sql
7. Backup Database Data Only (no table structure)
 # mysqldump -u root -p --no-create-info mydb > mydb.sql
8. Backup MySQL Database in XML Format
 # mysqldump -u root -p --xml mydb > mydb.xml
How to Restore MySQL Backup
For restoring databases from backup is quite simple. We use mysql command for it. For example following command will restore all backup from mydb.sql to mydb database.
# mysql -u root -p mydb < mydb.sql

No comments: