Introduction to MariaDB Mysqldump
MariaDB mysqldump is the utility provided in MariaDB using which you can take a backup of your current database so that when the situation arises of loss of data or inconsistent data due to unavoidable circumstances in a development environment, you can easily restore that data by using the dump file generated from mysqldump command and making sure the availability of the data to the user 24 * 7. In this article, we will firstly have a look at what is MariaDB mysqldump in detail and further study how we can use it for taking backup and restoration along with additional comments.
What is MariaDB Mysqldump?
When we make the use of mysqldump in MariaDB then a file gets generated which is actually having the flat-file format. The flat file contains the SQL statements which can be executed again to have the same database content as in the state when the backup was taken. Mysqldump can only be used if you as a user of that database have the privilege to access the database and entities of the tables such as tables and views.
If you can retrieve the data from MariaDB using, SELECT clause then surely you can make the use of mysqldump utility to generate a backup file. CSV, XML, or any other file which has a certain delimiter can also be produced by using mysqldump command. When we will execute the backup command at that time the database will be in the same state as it was while performing the mysqldump functionality which is done by using the execution of generated dump file.
We can make the use of mysqldump on a particular database only provided if you have the access privilege of SELECT command on the tables and views of the database that you want to dump and take a backup of. You can dump one or more than one database simultaneously. You can make the use of the following syntax for dumping the database in order to create a dump file for backup purposes in MariaDB –
For dumping one or more than one database present on the same server –
> mysqldump [required options] –databases database name … > sampleBackupFile.sql
For dumping all databases present on the server –
> mysqldump [required options] –all-databases > sampleBackupFile.sql
For dumping one or more tables of a database, use the below mysqldump syntax –
> mysqldump [required options] database name [table name …] > sampleBackupFile.sql
Make sure to fire the above command on the MariaDB shell or command prompt. The terminologies used in the above command are as discussed below in detail –
- Database name – This is the name of the database whose backup is supposed to be taken.
- sampleBackupFile – The name which you want your dump file to have. Note that it should have the extension of .sql only.
To get the complete details regarding required options used in the above syntax, you can type a quick command on the command prompt shown below –
Mysqldump -u root p -help that generates the output shown in the below image –
As you can observe from the above output that the list of the options used for specifying additional information that can be specified in mysqldump is very big, we can even go for exporting the same in a new file which can be done by making the use of the below command on command prompt –
mysqldump -u root p --help > temp
We can see that if we get a new command prompt after its execution the command has executed successfully creating a file containing the details of all options that can be specified in mysqldump command –
The file created with name temp looks as shown below after opening –
In order to restore the data that is backup by using mysqldump command, we can make the use of the restore command whose syntax is as shown below –
MySQL name of database < name of backup file.sql
- name of database – it is the database in which you want your data to restore.
- Name of the backup file – The file that was created by using mysqldump command.
How to Backup and Restore?
Let us now understand how we can actually implement backup and restore by using the mysqldump command with the help of one example. We have one database named educba which contains the table named developers stored in it. This table has some records. To check the complete contents of the developer’s table, let us fire the select query on the developer’s table –
Select * from educba.developers;
Note that previously we have already executed the use educba; command to make sure that our current database is educba. We can see that the developer’s table contains the following rows as its contents –
Consider that we have to take the backup of the educba database having only one table developer in it. This can be done by making the use of the following mysqldump command –
Mysqldump -u root -p –databases educba >backupOfEducba.sql
The execution of the above command gives the following output on a terminal and creating a file named backupOfEducba.sql containing all the commands which can be used for restoration purposes –
After opening the backupOfEducba.sql file, you can observe the following contents in it –
You can observe that the old existing tables are dropped, deleting all the previous structure of the table as well as its contents. After that, the table is recreated and data is inserted in it after locking the table to make sure that the data consistency is maintained.
Let us now see how we can restore the data on any other database with the help of the dump flat file generated from mysqldump command and restore command. Create the database named educba in a new server. Fire restore command. Our statement will be as shown below –
mysql -u root -p <backupOfEducba.sql
The output of the execution of the following command is as shown below –
After that, we can confirm the existence of the same tables in the educba database that we have created in a new database.
mysqldump comments – Mysqldump comment can be used only if you have the select privilege over the entity whose backup you will be taking. Whether it will be a database or the table in it. Proper syntax corresponding to the requirement of database or table to be dumped should be chosen for the dumping of mysqldump.
MariaDB mysqldump command is used for taking the backups of existing databases or databases and restoring when needed in case of data loss due to unavoidable circumstances.
This is a guide to MariaDB Mysqldump. Here we discuss the Introduction, What is MariaDB Mysqldump? examples with code implementation. You may also have a look at the following articles to learn more –