Administration toolsLinuxMySql

Backup and Restore MySql (MariaDB) database using mysqldump

Most of the time, you can easily manage your database with web-based tools such as phpMyAdmin. Unfortunately, there are times when you need to restore a large amount of data. This can’t be accomplished through phpMyAdmin because of PHP’s limits (either the maximum time a PHP script can use or the maximum file size upload).

Using the MySQL Command Line

Whether you are using MySQL on Linux or MS Windows, you can use the command line to perform most tasks. If you are using MS Windows, make sure that the path to the command line binaries is included in your system path. This will make things easier for you. Otherwise, you’ll have to type the full path to each command.

How To Backup A MySQL Database

MySQL provides a utility called “mysqldump.” Basically, this tool creates a flat file that contains the MySQL instructions to restore your database. Here are a few usage examples of mysqldump:

Creating a simple database backup

# mysqldump -u username -p database_name > file.sql

This creates a file that contains all of the MySQL statements to create tables and restore data into an existing database. If the target database contains tables with the same names, they will be overwritten. If you want the existing tables to be dropped and recreated, use the add-drop-table option:

# mysqldump --add-drop-table -u username -p database_name > file.sql

Alternately, you can choose to drop the whole database before recreating it and restoring data:

# mysqldump --add-drop-databases -u username -p database_name > file.sql

Backing Up Multiple Databases

You can backup multiple databases to a single file using the databases option:

# mysqldump -u username -p --databases database1 database2 database3 > file.sql

Creating a backup of all databases can be achieved using the all-databases option:

# mysqldump -u username -p --all-databases > file.sql

Backing Up InnoDB Tables

If your database has InnoDB tables, you will need to deactivate referential integrity while restoring data. Unfortunately, this can’t be done using the mysqldump utility. To do so, backup your database as you normally would. When done, open the MySQL file and add the following statement at the very beginning:

SET FOREIGN_KEY_CHECKS=0;

…and add the following at the end of the file:

SET FOREIGN_KEY_CHECKS=1;

Compressing MySQL Dump File

If you are using mysqldump in a Linux shell, you can pipe it through gzip to compress the dump file (assuming that you have gzip installed):

 # mysqldump -u username -p database_name | gzip -c file.sql.gz

Restoring A MySQL Backup

To restore a backup created with mysqldump, you will need to use the mysql command. If your MySQL dump file does not contain any “create database” statement, you can use the following command:

# mysql -u username -p database_name < file.sql

But, if it does, use the same command without specifying the database name:

# mysql -u username -p < file.sql

More information on mysqldump can be found here.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button