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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s