How to Backup and Restore a MySQL Database
Abstract: Backup data only from database (no table structure) mysqldump -u root -p --no-create-info mydb > mydb.sqlBackup database structure only (no data) mysq
MySQL is an relations database management system used for storing data in tabular format. 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.
This tutorial uses mysqldump command to backup mysql databases. The second is default mysql command for restoring databases from backup.
Backup MySQL Databases with mysqldumpYou have many options for creating databases backups. read few options below. For this example we have using databse name 「mydb」.
- Backup a single mysql database
mysqldump -u root -p mydb > mydb.sql
- Backup a single mysql database with archive
mysqldump -u root -p mydb | gzip > mydb.sql.gz
- Backup a single table from MySQL database
mysqldump -u root -p mydb tbl_student > tbl_student.sql
- Backup multiple databases in mysql
mysqldump -u root -p --databases mydb1 mydb2 mydb3 > mydb1-mydb2-mydb3.sql
- Backup all databases available in MySQL server.
mysqldump -u root -p --all-databases > all_databases.sql
- Backup database structure only (no data)
mysqldump -u root -p --no-data mydb > mydb.sql
- Backup data only from database (no table structure)
mysqldump -u root -p --no-create-info mydb > mydb.sql
- Backup MySQL database in XML format
mysqldump -u root -p --xml mydb > mydb.xml
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 need to specify database names during the restoration of all databases.
mysql -u root -p < all_databases.sql
Conclusion
In this tutorial, you have learned to various options to backup mysql databases using mysqldump.