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
Stop the
mysqldservice:# systemctl stop mysqld.serviceCreate a backup directory, for example:
# mkdir -p /root/mysqld-backup/{data,config}/Back up the data directory:
# cp -rp /var/lib/mysql/ /root/mysqld-backup/data/Back up the configuration files:
# cp -rp /etc/my.cnf /etc/my.cnf.d/ /root/mysqld-backup/config/Start the
mysqldservice:# 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
- You performed a file system backup on a MySQL server.
The target server must meet the following conditions of the backup source:
- The MySQL version must be identical or higher.
- The system architecture must be identical.
Procedure
Stop the
mysqldservice:# systemctl stop mysqld.serviceRemove the current
/var/lib/mysql/directory:# rm -rf /var/lib/mysql/Restore the data directory from your backup:
# cp -rp /root/mysqld-backup/data/mysql/ /var/lib/Ensure the correct ownership of the
/var/lib/mysql/directory:# chown -R mysql:mysql /var/lib/mysql/Restore the SELinux context of the
/var/lib/mysql/directory:# restorecon -Rv /var/lib/mysql/Remove the current configuration files:
# rm -rf /etc/my.cnf /etc/my.cnf.d/Restore the configuration files from your backup:
# cp -rp /root/mysqld-backup/config/my.cnf /root/mysqld-backup/config/my.cnf.d/ /etc/Ensure the correct ownership of the configuration files:
# chown -R root:root /etc/my.cnf /etc/my.cnf.d/Restore the SELinux context of the configuration files:
# restorecon -Rv /etc/my.cnf /etc/my.cnf.d/Start the
mysqldservice:# systemctl start mysqld.service
Verification
Connect to a MySQL database and query data, for example:
# mysql -u root -p <database> -e "*SELECT * FROM <table>;"