Introduction to MySQL Backup
The following article provides an outline for MySQL Backup. If you are using MySQL database in the production environment for your clients then you need to make sure that your database is always working fine and is available 24 * 7. For the availability of the database, you must often keep the backup of your client’s database. In case if the database is corrupted or is crashed or lost then you should be able to restore the data in the database.
For this reason, MySQL provides us with a facility to dump the database using mysqldump utility. This utility can only be used if your database is accessible and the select privilege on the tables of that database is assigned to you and the database is running. This utility creates a logical backup and a flat file containing the SQL statements that can be run again to bring back the database to the state when this file was created. This utility can be used for single or multiple database backup. mysqldump utility can also be used to produce the data in XML, CSV, or any other delimited text. You must dump your database frequently to have the updated backup of your database available to you. Whenever the backup is restored the database will be back to the state when that dump file was being created using mysqldump.
Pre-requisites for MySQL Backup
There are certain privileges on the tables, views, triggers, and transactions that you should have to use the mysqldump utility. It depends on the content you are backing up. If you are backing up the database that contains tables then you should have select privilege, for views it is necessary to have SHOW VIEW privilege, for triggers TRIGGER privilege and if we use –single-transaction option while dumping the database then LOCK TABLES privilege should be there with you.
Similarly, while reloading or restoring the dumped data, you must possess the privilege such as CREATE, INSERT, and ALTER privilege that might be present in your dumped flat file that will be executed. The ALTER statements may be present in the dumped file sometimes when stored programs are dumped for encoded character preservations. This ALTER command may result in the database collation changes and hence it is required to have ALTER privilege.
Syntax
We can create the backup by dumping one or more of the selected tables or by dumping a set of one or more databases or we can dump the entire MySQL server that will contain all databases and tables in it along with other objects. All three functionalities can be used by using the mysqldump command.
However, the syntax for each of them differs and is mentioned below:
- Dumping one or more of the selected tables:
mysqldump [specified_options] name_of_database [name_of_table...] > nameOfBackupFile.sql
- Dumping one or more of the selected databases:
mysqldump [specified_options] --databases name_of_database ... > nameOfBackupFile.sql
- Dumping complete MySQL server:
mysqldump [specified_options] --all-databases > nameOfBackupFile.sql
To see a complete list of the options that are available and can be used, we can execute the following command:
Command:
mysqldump -u root p –help
Output:
As the list is too big, you can export it to the file and then open the file to view the options and search for options that can be used in your context and use case. The output can be exported to file using the following command.
Command:
mysqldump -u root p --help > temp
And the temp file when opened on an editor looks like following:
Output:
Examples to Implement MySQL Backup
Let us consider a few examples, I will firstly query on my database server to display all databases:
Example #1
Command:
show databases;
Output:
Now, we will use educba database and check the tables present in it.
Command:
use educba;
show tables;
Output:
Let us now see all the records present in the developer’s table.
Command:
select * from developers;
Output:
Now, let us export the educba database using mysqldump command:
Command:
sudo mysqldump -u root -p --databases educba > backupOfEducba.sql
Output:
Note that you will have to exit from the MySQL command shell and then execute the above command. After, a file named backupOfEducba.sql file will be created on the same path. After opening the file, you will see that it contains all the commands of SQL that will recreate the educba database in case if we restore this file to a certain database.
Here’s how that file will look like:
This is the dumped flat-file created after dumping the educba file that contains the command of creating a database, create a table, and insert queries to insert the records in the table.
Example #2
Restoring the Database.
Let us now drop the database educba using the following command:
Command:
DROP DATABASE educba;
Output:
And now confirm the available databases by using the command:
Command:
show databases;
Output:
We can see that the educba database does not exist in our database server of MySQL.
Now, we will restore the educba database from the backup file backupOfEducba.sql that we created by dumping the educba database previously.
The restoration can be done by using the following command:
Command:
sudo mysql -u root -p backup_educba < backupOfEducba.sql
Output:
Alternatively, on your mysql command prompt, you can fire the following command to restore the database:
Command:
source /home/a/backupOfEducba .sql
Output:
Let us check the contents of backup_educba database:
We can see that the database named educba is created again and contains the same content as the developer’s table and all the records of that table.
Conclusion
We can backup the database or table using the mysqldump utility in MySQL. Different options assist us to use this utility program and the dumped flat file can be used at any time to restore the data. The restoration can be done easily using the terminal’s default command-prompt or mysql command prompt.
Recommended Articles
This is a guide to MySQL Backup. Here we discuss the introduction, Pre-requisites for MySQL backup and examples respectively. You can also go through our suggested articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses