Mysql Dump All Databases to File

MySQL Dump

Introduction to MySQL Dump

In case if the database of the system is corrupted or is crashed or lost then we 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 backups. mysqldump utility can also be used to produce the data in XML, CSV, or any other delimited text.

We must dump our database frequently to have the updated backup of the database available to us. Whenever the backup is restored the database will be back to the state when that dump file was being created using mysqldump.

Pre-requisites

There are certain privileges on the tables, views, triggers, and transactions that we should have to use the mysqldump utility. It depends on the content that we are backing up. If we 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 us.

Similarly, while reloading or restoring the dumped data, we 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 of MySQL Dump

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

Syntax of dumping complete mysql server are:

mysqldump [specified_options] --all-databases > nameOfBackupFile.sql

Many options we can use to specify the behavior or values of some of the objects like -u for the username using which we will login -p to mention the password and other options related to defining the behavior of dumping. There are many different types of options that can be specified. They are categorized into the following types –

  • Connection Options
  • Option-File Options
  • DDL Options
  • Debug Options
  • Help Options
  • Internationalization Options
  • Replication Options
  • Format Options
  • Filtering Options
  • Performance Options
  • Transactional Options

To see a complete list of the options that are available and can be used, we can execute the following command –

mysqldump -u root p –help

that gives the following output displaying all the options and usage of the same:

MySQL Dump-1.1

as the list is too big, you can export it tho 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 –

mysqldump -u root p --help > temp

Output:

MySQL Dump-1.2

And the temp file when opened on an editor looks like following:

MySQL Dump-1.3

Examples of MySQL Dump

Let us consider one example, we will firstly query on my database server to display all databases –

show databases;

Output:

MySQL Dump-2.1

Now, we will use educba database and check the tables present in it.

use educba;
show tables;

Output:

MySQL Dump-2.2

Let us now see all the records present in developers table.

select * from developers;

Output:

MySQL Dump-2.3

Now, let us export the educba database using mysqldump command –

mysqldump -u root -p --databases educba > backupOfEducba.sql

Output:

MySQL Dump-2.4

Note that we 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, we 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:

MySQL Dump-2.5

This is the dumped flat-file created after dumping the educba file that contains the command of create a database, create a table and insert queries to insert the records in the table.

Restoring the Database

Let us now drop the database educba using the following command –

DROP DATABASE educba;

Output:

MySQL Dump-3.1

And now confirm the available databases by using the command –

show databases;

Output:

MySQL Dump-3.2

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:

sudo mysql -u root -p < backupOfEducba.sql

Output:

MySQL Dump-3.3

Let us check the contents of backup_educba database

show database;
use educba;
MySQL
select * from developers;

MySQL Dump-3.4

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.

Recommended Articles

This is a guide to MySQL Dump. Here we also discuss the introduction and syntax of mysql dump along with different examples and its code implementation. you may also have a look at the following articles to learn more –

  1. Unique Key in MySQL
  2. MySQL IN Operator
  3. MySQL Subquery
  4. IF Statement in MySQL

Mysql Dump All Databases to File

Source: https://www.educba.com/mysql-dump/

0 Response to "Mysql Dump All Databases to File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel