MySQL backup and restore database

Table of contents

Prerequisites

  • MySQL or MariaDB installed
  • A database

Dumping the database

The dump includes commands to restore tables and data. If you store views it'll store the user permissions related to them, in that case edit the dump file and update the user accordingly.

To effectively backup your database run the following command:

 mysqldump -u [user] -p [database_name] > [dump].sql
  • Replace the [user] with the user that has permissions to read the database.
  • Replace [database_name] with the name of the database.
  • Replace [dump] with the desired name of the dump.

Restoring the database

To restore a database, replace [user] with the user that has access to the database, and replace [database] with the name of the database in which you which to include the dump content.

mysql -p -u [user] [database] <  [dump].sql

If the dump contains multiple databases you can omit [database] as follows:

mysql -p -u [user] < [dump].sql
Related Content
Author
I’m a passionate full-stack software engineer, architect and Debian lover.