How to backup and restore databases in Mysql or Mariadb

In this guide we will learn how to back up and restore – inporting and exporting data in Mysql or Mariadb – the commands are interchangeable.

Importing and exporting databases is a common task in software development. You can use data dumps to back up and restore your information. You can also use them to migrate data to a new server or development environment.

Prerequisites

To follow along, you need the following:

  • Access to a mysql or mariadb server
  • Terminal access with mysql command installed
  • A database in the db server created

Table of Content

  1. Exporting a database
  2. Importing a database

1. Exporting a databasee

The mysqldump console utility exports databases to SQL text files. This makes it easier to transfer and move databases. You will need your database’s name and credentials for an account whose privileges allow at least full read-only access to the database.

Use mysqldump to export your database:

mysqldump -u root -p citizix_db > <meta charset="utf-8">citizix_db.sql

 Copy

  • root is the username you can log in to the database with
  • citizix_db is the name of the database to export
  • citizix_db.sql is the file in the current directory that stores the output.

The command will produce no visual output, but you can inspect the contents of citizix_db.sql to check if it’s a legitimate SQL dump file.

$ ls | grep sql
citizix_db.sql

Run the following command:

head -n 5 <meta charset="utf-8">citizix_db.sql

The top of the file should look similar to this, showing a MySQL dump for a database named database_name.

# head -n 5 citizix_db.sql
-- MariaDB dump 10.19  Distrib 10.7.1-MariaDB, for debian-linux-gnu (x86_64)
--
-- Host: localhost    Database: citizix_db
-- ------------------------------------------------------
-- Server version	10.7.1-MariaDB-1:10.7.1+maria~focal

If any errors occur during the export process, mysqldump will print them to the screen.

2. Importing a database

Log in to MySQL as root or another user with sufficient privileges to create new databases:

mysql -u root -p

Type in your password. This command will bring you into the MySQL shell prompt.

$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 349
Server version: 10.7.1-MariaDB-1:10.7.1+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

If you don’t have a database in your case, you can create a database using this command:

create database database_name;

I have a database citizix_db in my case that I will use for this example. Check the databases with this command:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| citizix_db         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.005 sec)

The export operations will be done fom the bash shell so exit the MySQL shell by pressing CTRL+D. From the normal command line, you can import the dump file with the following command:

mysql -u root -p citizix_db < <meta charset="utf-8">citizix_db.sql
  • root is the username you can log in to the database with
  • citizix_db is the name of the freshly created database
  • citizix_db.sql is the data dump file to be imported, located in the current directory

If the command runs successfully, it won’t produce any output. If any errors occur during the process, mysql will print them to the terminal instead. To check if the import was successful, log in to the MySQL shell and inspect the data. Selecting the new database with USE new_database and then use SHOW TABLES; or a similar command to look at some of the data.

Conclusion

In this tutorial we learnt how to import and export a MySQL or MariaDB database. mysqldump has additional settings that you can use to alter how the system creates data dumps. You can learn more about from the official mysqldump documentation page.

Last updated on Oct 14, 2024 11:46 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy