7.2 Database Backup Methods数据库备份方法

This section summarizes some general methods for making backups.本节总结了一些进行备份的一般方法。

Making a Hot Backup with MySQL Enterprise Backup使用MySQL企业备份进行热备份

Customers of MySQL Enterprise Edition can use the MySQL Enterprise Backup product to do physical backups of entire instances or selected databases, tables, or both. MySQL Enterprise Edition的客户可以使用MySQL Enterprise Backup产品对整个实例或选定的数据库、表或两者进行物理备份。This product includes features for incremental and compressed backups. 此产品包括增量备份压缩备份功能。Backing up the physical database files makes restore much faster than logical techniques such as the mysqldump command. 备份物理数据库文件使恢复比mysqldump命令等逻辑技术快得多。InnoDB tables are copied using a hot backup mechanism. InnoDB表是使用热备份机制复制的。(Ideally, the InnoDB tables should represent a substantial majority of the data.) (理想情况下,InnoDB表应该代表大部分数据。)Tables from other storage engines are copied using a warm backup mechanism. For an overview of the MySQL Enterprise Backup product, see Section 30.2, “MySQL Enterprise Backup Overview”.使用热备份机制复制其他存储引擎中的表。有关MySQL企业备份产品的概述,请参阅第30.2节,“MySQL企业备份概述”

Making Backups with mysqldump使用mysqldump进行备份

The mysqldump program can make backups. It can back up all kinds of tables. (See Section 7.4, “Using mysqldump for Backups”.)mysqldump程序可以进行备份。它可以备份各种表格。(请参阅第7.4节,“使用mysqldump进行备份”。)

For InnoDB tables, it is possible to perform an online backup that takes no locks on tables using the --single-transaction option to mysqldump. See Section 7.3.1, “Establishing a Backup Policy”.对于InnoDB表,可以使用mysqldump的--single-transaction选项执行不锁定表的在线备份。请参阅第7.3.1节,“建立备份策略”

Making Backups by Copying Table Files通过复制表文件进行备份

MyISAM tables can be backed up by copying table files (*.MYD, *.MYI files, and associated *.sdi files). 可以通过复制表文件(*.MYD*.MYI文件和关联的*.sdi文件)来备份MyISAM表。To get a consistent backup, stop the server or lock and flush the relevant tables:要获得一致的备份,请停止服务器或锁定并刷新相关表:

FLUSH TABLES tbl_list WITH READ LOCK;

You need only a read lock; this enables other clients to continue to query the tables while you are making a copy of the files in the database directory. 你只需要一个读锁;这使其他客户端能够在您复制数据库目录中的文件时继续查询表。The flush is needed to ensure that the all active index pages are written to disk before you start the backup. See Section 13.3.6, “LOCK TABLES and UNLOCK TABLES Statements”, and Section 13.7.8.3, “FLUSH Statement”.需要刷新以确保在启动备份之前将所有活动索引页写入磁盘。参见第13.3.6节,“LOCK TABLES和UNLOCK TABLESs语句”第13.7.8.3节,“FLUSH语句”

You can also create a binary backup simply by copying the table files, as long as the server isn't updating anything. 只要服务器不更新任何内容,您也可以通过复制表文件来创建二进制备份。(But note that table file copying methods do not work if your database contains InnoDB tables. (但请注意,如果数据库包含InnoDB表,则表文件复制方法不起作用。Also, even if the server is not actively updating data, InnoDB may still have modified data cached in memory and not flushed to disk.)此外,即使服务器没有主动更新数据,InnoDB仍可能将修改后的数据缓存在内存中,而不会刷新到磁盘。)

For an example of this backup method, refer to the export and import example in Section 13.2.5, “IMPORT TABLE Statement”.有关此备份方法的示例,请参阅第13.2.5节,“IMPORT TABLE语句”中的导出和导入示例。

Making Delimited-Text File Backups进行分隔文本文件备份

To create a text file containing a table's data, you can use SELECT * INTO OUTFILE 'file_name' FROM tbl_name. 要创建包含表数据的文本文件,可以使用SELECT * INTO OUTFILE 'file_name' FROM tbl_nameThe file is created on the MySQL server host, not the client host. For this statement, the output file cannot already exist because permitting files to be overwritten constitutes a security risk. 该文件是在MySQL服务器主机上创建的,而不是在客户端主机上。对于此语句,输出文件不可能已经存在,因为允许覆盖文件会构成安全风险。See Section 13.2.10, “SELECT Statement”. This method works for any kind of data file, but saves only table data, not the table structure.请参阅第13.2.10节,“SELECT语句”。此方法适用于任何类型的数据文件,但只保存表数据,不保存表结构。

Another way to create text data files (along with files containing CREATE TABLE statements for the backed up tables) is to use mysqldump with the --tab option. See Section 7.4.3, “Dumping Data in Delimited-Text Format with mysqldump”.创建文本数据文件(以及包含用于备份表的CREATE TABLE语句的文件)的另一种方法是使用带有--tab选项的mysqldump。请参阅第7.4.3节,“使用mysqldump以分隔文本格式转储数据”

To reload a delimited-text data file, use LOAD DATA or mysqlimport.要重新加载分隔的文本数据文件,请使用LOAD DATAmysqlimport

Making Incremental Backups by Enabling the Binary Log通过启用二进制日志进行增量备份

MySQL supports incremental backups using the binary log. The binary log files provide you with the information you need to replicate changes to the database that are made subsequent to the point at which you performed a backup. MySQL支持使用二进制日志进行增量备份。二进制日志文件为您提供了将在执行备份后所做的更改复制到数据库所需的信息。Therefore, to allow a server to be restored to a point-in-time, binary logging must be enabled on it, which is the default setting for MySQL 8.0 ; see Section 5.4.4, “The Binary Log”.因此,为了允许服务器恢复到某个时间点,必须在其上启用二进制日志记录,这是MySQL 8.0的默认设置;参见第5.4.4节,“二进制日志”

At the moment you want to make an incremental backup (containing all changes that happened since the last full or incremental backup), you should rotate the binary log by using FLUSH LOGS. 当您想要进行增量备份(包含自上次完全备份或增量备份以来发生的所有更改)时,应该使用FLUSH LOGS旋转二进制日志。This done, you need to copy to the backup location all binary logs which range from the one of the moment of the last full or incremental backup to the last but one. 完成后,您需要将所有二进制日志复制到备份位置,这些日志的范围从上一次完整或增量备份的时刻到倒数第二次。These binary logs are the incremental backup; at restore time, you apply them as explained in Section 7.5, “Point-in-Time (Incremental) Recovery”. 这些二进制日志是增量备份;在恢复时,您可以按照第7.5节,“时间点(增量)恢复”中的说明应用它们。The next time you do a full backup, you should also rotate the binary log using FLUSH LOGS or mysqldump --flush-logs. 下次进行完整备份时,还应该使用FLUSH LOGSmysqldump --flush-logs旋转二进制日志。See Section 4.5.4, “mysqldump — A Database Backup Program”.请参阅第4.5.4节,“mysqldump-数据库备份程序”

Making Backups Using Replicas使用复制副本进行备份

If you have performance problems with a server while making backups, one strategy that can help is to set up replication and perform backups on the replica rather than on the source. See Section 17.4.1, “Using Replication for Backups”.如果在进行备份时服务器出现性能问题,一种可以提供帮助的策略是在复制副本而不是源上设置复制并执行备份。请参阅第17.4.1节,“使用复制进行备份”

If you are backing up a replica, you should back up its connection metadata repository and applier metadata repository (see Section 17.2.4, “Relay Log and Replication Metadata Repositories”) when you back up the replica's databases, regardless of the backup method you choose. 如果要备份复制副本,无论选择何种备份方法,都应在备份复制副本的数据库时备份其连接元数据存储库和应用程序元数据存储库(请参阅第17.2.4节,“中继日志和复制元数据存储库”)。This information is always needed to resume replication after you restore the replica's data. 在恢复复制副本的数据后,恢复复制始终需要此信息。If your replica is replicating LOAD DATA statements, you should also back up any SQL_LOAD-* files that exist in the directory that the replica uses for this purpose. 如果您的复制副本正在复制LOAD DATA语句,您还应该备份复制副本用于此目的的目录中存在的任何SQL_LOAD-*文件。The replica needs these files to resume replication of any interrupted LOAD DATA operations. 复制副本需要这些文件来恢复任何中断的LOAD DATA操作的复制。The location of this directory is the value of the slave_load_tmpdir system variable. 此目录的位置是slave_load_tmpdir系统变量的值。If the server was not started with that variable set, the directory location is the value of the tmpdir system variable.如果服务器不是用该变量集启动的,那么目录位置就是tmpdir系统变量的值。

Recovering Corrupt Tables恢复损坏的表

If you have to restore MyISAM tables that have become corrupt, try to recover them using REPAIR TABLE or myisamchk -r first. 如果必须恢复已损坏的MyISAM表,请先尝试使用REPAIR TABLEmyisamchk -r恢复它们。That should work in 99.9% of all cases. 这应该在99.9%的情况下有效。If myisamchk fails, see Section 7.6, “MyISAM Table Maintenance and Crash Recovery”.如果myisamchk失败,请参阅第7.6节,“MyISAM表维护和故障恢复”

Making Backups Using a File System Snapshot使用文件系统快照进行备份

If you are using a Veritas file system, you can make a backup like this:如果您使用的是Veritas文件系统,则可以进行如下备份:

  1. From a client program, execute FLUSH TABLES WITH READ LOCK.从客户端程序中,执行FLUSH TABLES WITH READ LOCK

  2. From another shell, execute mount vxfs snapshot.从另一个shell执行mount vxfs snapshot

  3. From the first client, execute UNLOCK TABLES.从第一个客户端执行UNLOCK TABLES

  4. Copy files from the snapshot.从快照中复制文件。

  5. Unmount the snapshot.卸载快照。

Similar snapshot capabilities may be available in other file systems, such as LVM or ZFS.类似的快照功能可能在其他文件系统中可用,例如LVM或ZFS。