How to Backup (Export) and Restore (Import) MySQL or MariaDB Database

How to Backup (Export) and Restore (Import) MySQL or MariaDB Database

December 09,2010 by MAhmadZ

How to Backup (Export) and Restore (Import) MySQL or MariaDB Database

Backing-up and Restoring databases in MySQL is a very straight forward process using the terminal.

To Backup a Database

mysqldump -u [username] --password=[passwd] [database] > [file].sql

This will create the file in the same directory. This is basically a text file and can be opened using any text editor.

To Restore a Database

mysql -u [user_name] --password=[your_password] [database] < [file_name].sql

This will restore all the data and tables to the existing Database that has been mentioned.

To Import a new Database

mysql -u [user_name] --password=[your_password] < [file_name].sql

Simply skipping the name of the existing database will create the new one based on the [file_name].sql file provided.

Note:

To can skip the –password=[your_password] in all of these commands with -p switch and it will ask for the password later. From a security point of view this would be better as password as plain text won’t be stored in the .mysql_history file.