Updated June 3, 2023
Introduction to MariaDB Mysqldump
MariaDB provides the utility mysqldump, this enables you to create a backup of your current database. This backup is helpful in scenarios where data loss or inconsistent data occurs due to unavoidable circumstances in a development environment. Using the dump file generated from the mysqldump command, you can easily restore the data and ensure its availability to the user 24/7. In this article, we will first look at what MariaDB mysqldump is in detail and further study how we can use it for backup and restoration, along with additional comments.
What is MariaDB Mysqldump?
When using mysqldump in MariaDB, it generates a flat-file format file that contains SQL statements. Execute these statements to restore the database to its state as it was when the backup was created. To use mysqldump, you need the necessary privileges to access the database and its entities, including tables and views.
If you can retrieve the data from MariaDB using the SELECT clause, you can surely use the mysqldump utility to generate a backup file. You can also generate files in CSV, XML, or any other format with a specific delimiter using the mysqldump command. When you execute the backup command, the database will be restored to the same state as when the mysqldump operation was performed using the generated dump file.
We can use mysqldump on a particular database only provided 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 databases simultaneously. You can make use of the following syntax for dumping the database 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
Fire the above command on the MariaDB shell or command prompt. Let’s discuss in detail the terminologies used in the command above:
- Database name – This represents the database name for which you want to take the backup.
- sampleBackupFile – The name which you want your dump file to have. Note that it should have the extension of .sql only.
To obtain complete details about the options used in the above syntax, you can execute the following command on the command prompt:
Mysqldump -u root p -help that generates the output shown in the below image –
As you can observe from the above output, the list of the options used for specifying additional information that can be specified in mysqldump is huge; we can even go for exporting the same in a new file which can be done by making 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 the mysqldump command –
The file created with name temp looks as shown below after opening –
To restore the data that is backup by using the mysqldump command, we can make use of the restore command, whose syntax is as shown below –
MySQL name of database < name of backup file.sql
- name of the database is the database you want your data to restore.
- Name of the backup file – The file created using the mysqldump command.
How to Backup and Restore?
Let us now understand how we can 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 we have already executed the educba; command to ensure 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. You can achieve this by utilizing the following mysqldump command:
Mysqldump -u root -p –databases educba >backupOfEducba.sql
When you execute the above command, it will generate the following output on the terminal and create a file named “backupOfEducba.sql”. This file will contain all the commands required for restoring the database:
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 structures of the table and its contents. After that, the table is recreated, and data is inserted in it after locking it to maintain data consistency.
Let us now see how we can restore the data on any other database with the help of the dump flat file generated from the 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 – You can use mysqldump comments only if you have the select privilege over the entity for which you will be taking the backup.Whether it will be a database or a table in it. You should choose the appropriate syntax that corresponds to the requirement of the database or table you want to dump when using mysqldump.
MariaDB mysqldump command is used to backup existing databases or databases and restore them when needed in case of data loss due to unavoidable circumstances.
We hope that this EDUCBA information on “MariaDB Mysqldump” was beneficial to you. You can view EDUCBA’s recommended articles for more information.