1.6. Backing up and restoring MariaDB data with logical dumps


A logical backup of MariaDB data consists of the SQL statements necessary to restore the data. The advantage of logical backup over physical backup is that the data can be restored on other hardware configurations and MariaDB versions.

1.6.1. Performing a logical backup by using mariadb-dump

You can use the mariadb-dump utility to back up databases while the MariaDB server is running and store the exported data in an SQL file. Store the backup in a safe location to be able to recover it in data loss scenarios.

Frequent scenarios with mariadb-dump include:

  • Backing up a single database
  • Backing up multiple databases
  • Backing up all databases

The mariadb-dump utility stores its output in a single file. If you want to back up multiple databases and require one file per database, back up each database individually.

注意

The mariadb-dump utility can back up only databases. This also includes server settings stored in the mysql database. However, the utility does not back up configuration files, such as /etc/my.cnf.

Prerequisites

  • The mariadb service is running.
  • You have credentials with permissions to back up a database, for example, the root account.

Procedure

  • Create a consistent and comprehensive logical backup of MariaDB databases:

    # mariadb-dump -u <username> -p --routines --events --triggers --single-transaction --result-file=backup.sql --databases <database_1> <database_2>

    where:

    -u <username>
    Sets the username the utility uses to connect to the database server.
    -p
    Prompts for the password.
    --routines
    Includes stored procedures and functions in the backup.
    --events
    Includes scheduled events in the backup.
    --triggers
    Includes triggers in the backup.
    --single-transaction

    Initiates a consistent snapshot of databases with a transactional storage engine, such as InnoDB. By using a single transaction, all read operations reflect the database state at the moment the dump begins.

    If you still use a non-transactional storage engine, such as MyISAM, use the --lock-tables option instead of --single-transaction to ensure a consistent backup.

    --result-file=<output_file>
    Defines in which file mariadb-dump stores the output.
    --databases <list_of_databases>

    Defines the databases to back up. Alternatively, to back up all databases at once, use the --all-databases option.

    重要

    A backup of a database contains only the data from that database. It does not include MariaDB user accounts or other server settings. MariaDB stores this essential security and system information in a separate mysql system database. Therefore, you must back up mysql as well if you need to preserve these settings.

Verification

  • Restore the backup in a sandbox environment and ensure that the data is correct.

1.6.2. Restoring MariaDB data from a dump in SQL format

If you backed up one or multiple databases to an SQL file, you can use this file to re-create the database structure and its data.

Prerequisites

  • The mariadb service is running.
  • You have credentials with permissions to restore data, for example, the root account.

Procedure

  1. If the database you want to restore already exists and your SQL file does not contain DROP TABLE IF EXISTS statements, you must manually remove the tables or the entire database:

    • To remove a table, enter:

      # mariadb -u root -p -e "DROP TABLE <database>.<table>;"

      Repeat this command for every table that the SQL file will re-create.

    • To remove a database, enter:

      # mariadb -u root -p -e "DROP DATABASE <database>;"

      Repeat this command for every database that the SQL file will re-create.

  2. Import the SQL file:

    # mariadb -u root -p < backup.sql"

Verification

  • Connect to a MariaDB database and query data, for example:

    # mariadb -u root -p <database> -e "*SELECT * FROM <table>;"
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部