2.7. Backing up and restoring MySQL data with physical copies


A physical backup of MySQL data contains files and directories that store the content. This method is typically faster and smaller in size.

2.7.1. Performing a file system backup on a MySQL server

A file system-level backup is a fast way to back up a complete MySQL instance. This method requires a shutdown of the mysqld service for data consistency.

重要

A file system-level backup is specific to an architecture and MySQL version. You cannot restore data backed up by this method on a different architecture or MySQL version.

Procedure

  1. Stop the mysqld service:

    # systemctl stop mysqld.service
  2. Create a backup directory, for example:

    # mkdir -p /root/mysqld-backup/{data,config}/
  3. Back up the data directory:

    # cp -rp /var/lib/mysql/ /root/mysqld-backup/data/
  4. Back up the configuration files:

    # cp -rp /etc/my.cnf /etc/my.cnf.d/ /root/mysqld-backup/config/
  5. Start the mysqld service:

    # systemctl start mysqld.service

2.7.2. Restoring a file system backup on a MySQL server

If your MySQL instance has been corrupted, and you previously performed a file system backup that includes the data directory and the configuration files, you can restore the instance from this backup.

Prerequisites

Procedure

  1. Stop the mysqld service:

    # systemctl stop mysqld.service
  2. Remove the current /var/lib/mysql/ directory:

    # rm -rf /var/lib/mysql/
  3. Restore the data directory from your backup:

    # cp -rp /root/mysqld-backup/data/mysql/ /var/lib/
  4. Ensure the correct ownership of the /var/lib/mysql/ directory:

    # chown -R mysql:mysql /var/lib/mysql/
  5. Restore the SELinux context of the /var/lib/mysql/ directory:

    # restorecon -Rv /var/lib/mysql/
  6. Remove the current configuration files:

    # rm -rf /etc/my.cnf /etc/my.cnf.d/
  7. Restore the configuration files from your backup:

    # cp -rp /root/mysqld-backup/config/my.cnf /root/mysqld-backup/config/my.cnf.d/ /etc/
  8. Ensure the correct ownership of the configuration files:

    # chown -R root:root /etc/my.cnf /etc/my.cnf.d/
  9. Restore the SELinux context of the configuration files:

    # restorecon -Rv /etc/my.cnf /etc/my.cnf.d/
  10. Start the mysqld service:

    # systemctl start mysqld.service

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
返回顶部