2.6. Backing up and restoring MySQL data with logical dumps


A logical backup of MySQL 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 MySQL versions.

2.6.1. Performing a logical backup by using mysqldump

The mysqldump utility is a backup utility that can export one or more databases. The output of mysqldump typically consists of SQL statements to re-create the server table structure, populate it with data, or both.

To perform the mysqldump backup, you can use one of the following options:

  • Back up one or more selected databases
  • Back up all databases
  • Back up a subset of tables from one database

Procedure

  • To dump a single database, run:

    # mysqldump [options] --databases db_name > backup-file.sql
  • To dump multiple databases at once, run:

    # mysqldump [options] --databases db_name1 [db_name2 ...] > backup-file.sql
  • To dump all databases, run:

    # mysqldump [options] --all-databases > backup-file.sql
  • To load one or more dumped full databases back into a server, run:

    # mysql < backup-file.sql
  • To load a database to a remote MySQL server, run:

    # mysql --host=remote_host < backup-file.sql
  • To dump a subset of tables from one database, add a list of the chosen tables at the end of the mysqldump command:

    # mysqldump [options] db_name [tbl_name ...​] > backup-file.sql
  • To load a subset of tables dumped from one database, run:

    # mysql db_name < backup-file.sql
    注意

    The db_name database must exist at this point.

  • To see a list of the options that mysqldump supports, run:

    $ mysqldump --help

2.6.2. Restoring MySQL 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 mysqld 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:

      # mysql -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:

      # mysql -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:

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

Verification

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

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

学习

尝试、购买和销售

社区

關於紅帽

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

让开源更具包容性

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

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部