Featured image of post How to backup MySQL & MariaDB database using mysqldump

How to backup MySQL & MariaDB database using mysqldump

Learn how to take a backup of your database for backup and migration, and how to restore a database to a new server.

Backing up your website database before major changes or updates is important step, and there are many different ways to backup a database using GUI or CLI. In this article you will learn how to use a utility called mysqldump to backup a MySQL or MariaDB database. The same instructions work for both database engines.

First, you need to identify the database you are backing up. Login to mysql to show all current databases using the following commands:

mysql -u root -p

show databases;

You should get an output similar to this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| bitnami_wordpress |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)

The database of interest is called bitnami_wordpress in my case, so I just need to enter the following mysqldump command to take a snapshot of it:

1
mysqldump -u root -p bitnami_wordpress > backup.sql

Finally you can download the backup.sql output file using scp or an SFTP client like FileZilla.

To restore the database backup file:

  1. Upload the backup.sql to the new server.
  2. Navigate to the directory where your uploaded backup.sql.
  3. Use the following mysql command to import the backup file into a database named bitnami_wordpress:
1
mysql -u root -p -D bitnami_wordpress < mysqldump7-7-23.sql

If the new database already has data, you can wipe it first, then re-create it, and finish by using mysql command for importing the backup to a clean empty database:

1
2
3
4
mysql -u root -p
mysql> drop database bitnami_wordpress;
mysql> create database bitnami_wordpress;
mysql -u root -p -D bitnami_wordpress < mysqldump7-7-23.sql
Built with Hugo
Theme Stack designed by Jimmy