双写缓冲区是一个存储区域,InnoDB 在将从缓冲池刷写的页面写入 InnoDB 数据文件的正确位置之前,会先将页面写入该区域。如果在页面写入过程中出现操作系统、存储子系统或意外的 mysqld 进程退出,InnoDB 可以在崩溃恢复期间从双写缓冲区找到页面的正确副本。The doublewrite buffer is a storage area where
InnoDB writes pages flushed from the buffer
pool before writing the pages to their proper positions in the
InnoDB data files. If there is an operating
system, storage subsystem, or unexpected mysqld
process exit in the middle of a page write,
InnoDB can find a good copy of the page from
the doublewrite buffer during crash recovery.
虽然数据被写入两次,但双写缓冲区并不需要双倍的 I/O 开销或双倍的 I/O 操作。数据以大块顺序写入双写缓冲区,并通过单次 fsync() 调用操作系统(除非 innodb_flush_method 设置为 O_DIRECT_NO_FSYNC)。Although data is written twice, the doublewrite buffer does not
require twice as much I/O overhead or twice as many I/O
operations. Data is written to the doublewrite buffer in a large
sequential chunk, with a single fsync() call to
the operating system (except in the case that
innodb_flush_method is set to
O_DIRECT_NO_FSYNC).
双写缓冲区的存储区域位于双写文件中。The doublewrite buffer storage area is located in doublewrite files.
以下变量用于双写缓冲区配置:The following variables are provided for doublewrite buffer configuration:
innodb_doublewrite变量控制双写缓冲区是否启用。在大多数情况下,它默认是启用的。要禁用双写缓冲区,将innodb_doublewrite设置为OFF。如果您更关心性能而非数据完整性,例如在进行基准测试时,可以考虑禁用双写缓冲区。Theinnodb_doublewritevariable controls whether the doublewrite buffer is enabled. It is enabled by default in most cases. To disable the doublewrite buffer, setinnodb_doublewritetoOFF. Consider disabling the doublewrite buffer if you are more concerned with performance than data integrity, as may be the case when performing benchmarks, for example.innodb_doublewrite支持DETECT_AND_RECOVER和DETECT_ONLY设置。innodb_doublewritesupportsDETECT_AND_RECOVERandDETECT_ONLYsettings.DETECT_AND_RECOVER设置与ON设置相同。使用此设置时,双写缓冲区将完全启用,数据库页面内容会写入双写缓冲区,在恢复过程中访问该缓冲区以修复未完成的页面写入。TheDETECT_AND_RECOVERsetting is the same as theONsetting. With this setting, the doublewrite buffer is fully enabled, with database page content written to the doublewrite buffer where it is accessed during recovery to fix incomplete page writes.使用
DETECT_ONLY设置时,仅将元数据写入双写缓冲区。数据库页面内容不会写入双写缓冲区,恢复过程也不会使用双写缓冲区来修复未完成的页面写入。此轻量级设置仅用于检测未完成的页面写入。With theDETECT_ONLYsetting, only metadata is written to the doublewrite buffer. Database page content is not written to the doublewrite buffer, and recovery does not use the doublewrite buffer to fix incomplete page writes. This lightweight setting is intended for detecting incomplete page writes only.MySQL 支持对
innodb_doublewrite设置进行动态更改,用于启用双写缓冲区,可在ON、DETECT_AND_RECOVER和DETECT_ONLY之间切换。MySQL 不支持在启用双写缓冲区的设置与OFF之间或反之进行动态更改。MySQL supports dynamic changes to theinnodb_doublewritesetting that enables the doublewrite buffer, betweenON,DETECT_AND_RECOVER, andDETECT_ONLY. MySQL does not support dynamic changes between a setting that enables the doublewrite buffer andOFFor vice versa.如果双写缓冲区位于支持原子写入的 Fusion-io 设备上,双写缓冲区将自动禁用,并且数据文件写入将改为使用 Fusion-io 的原子写入。然而,请注意
innodb_doublewrite设置是全局的。当双写缓冲区被禁用时,它对于所有数据文件都是禁用的,包括那些不在 Fusion-io 硬件上的文件。此功能仅在 Fusion-io 硬件上受支持,并且仅在 Linux 上的 Fusion-io NVMFS 上启用。为了充分利用此功能,建议将innodb_flush_method设置为O_DIRECT。If the doublewrite buffer is located on a Fusion-io device that supports atomic writes, the doublewrite buffer is automatically disabled and data file writes are performed using Fusion-io atomic writes instead. However, be aware that theinnodb_doublewritesetting is global. When the doublewrite buffer is disabled, it is disabled for all data files including those that do not reside on Fusion-io hardware. This feature is only supported on Fusion-io hardware and is only enabled for Fusion-io NVMFS on Linux. To take full advantage of this feature, aninnodb_flush_methodsetting ofO_DIRECTis recommended.innodb_doublewrite_dir变量定义了InnoDB创建双写文件的目录。如果未指定目录,双写文件将创建在innodb_data_home_dir目录中,如果未指定则默认为数据目录。Theinnodb_doublewrite_dirvariable defines the directory whereInnoDBcreates doublewrite files. If no directory is specified, doublewrite files are created in theinnodb_data_home_dirdirectory, which defaults to the data directory if unspecified.为避免与模式名称冲突,指定的目录名称前会自动加上哈希符号 '#'。但是,如果在目录名称中明确指定了 '.', '#' 或 '/' 前缀,则不会在目录名称前加上哈希符号 '#'A hash symbol '#' is automatically prefixed to the specified directory name to avoid conflicts with schema names. However, if a '.', '#'. or '/' prefix is specified explicitly in the directory name, the hash symbol '#' is not prefixed to the directory name.
理想情况下,doublewrite 目录应放置在最快的存储介质上。Ideally, the doublewrite directory should be placed on the fastest storage media available.
innodb_doublewrite_filesinnodb_doublewrite_filesinnodb_doublewrite_files变量定义了 doublewrite 文件的数量,默认为 2。默认情况下,每个缓冲池实例会创建两个 doublewrite 文件:一个用于 flush 列表的 doublewrite 文件和一个用于 LRU 列表的 doublewrite 文件。Theinnodb_doublewrite_filesvariable defines the number of doublewrite files, which defaults to 2. By default, two doublewrite files are created for each buffer pool instance: A flush list doublewrite file and an LRU list doublewrite file.flush 列表 doublewrite 文件用于从缓冲池 flush 列表中刷出的页面。flush 列表 doublewrite 文件的默认大小为
InnoDB页面大小 * doublewrite 页面字节数。The flush list doublewrite file is for pages flushed from the buffer pool flush list. The default size of a flush list doublewrite file is theInnoDBpage size * doublewrite page bytes.LRU 列表双写文件用于从缓冲池 LRU 列表中刷新的页面。它还包含用于单页刷新的槽。LRU 列表双写文件的默认大小为
InnoDB页面大小 * (双写页面数 + (512 / 缓冲池实例数)),其中 512 是为单页刷新保留的槽总数。The LRU list doublewrite file is for pages flushed from the buffer pool LRU list. It also contains slots for single page flushes. The default size of an LRU list doublewrite file is theInnoDBpage size * (doublewrite pages + (512 / the number of buffer pool instances)) where 512 is the total number of slots reserved for single page flushes.至少有两个双写文件。双写文件的最大数量是缓冲池实例数量的两倍。(缓冲池实例的数量由
innodb_buffer_pool_instances变量控制。)At a minimum, there are two doublewrite files. The maximum number of doublewrite files is two times the number of buffer pool instances. (The number of buffer pool instances is controlled by theinnodb_buffer_pool_instancesvariable.)双写文件名具有以下格式:
#ib_(在page_size_file_number.dblwrDETECT_ONLY设置下为.bdblwr)。例如,对于具有 16KBInnoDB页面大小和单个缓冲池的 MySQL 实例,将创建以下双写文件:Doublewrite file names have the following format:#ib_(orpage_size_file_number.dblwr.bdblwrwith theDETECT_ONLYsetting). For example, the following doublewrite files are created for a MySQL instance with anInnoDBpages size of 16KB and a single buffer pool:#ib_16384_0.dblwr #ib_16384_1.dblwr
innodb_doublewrite_files变量用于高级性能调整。默认设置对于大多数用户来说应该是合适的。Theinnodb_doublewrite_filesvariable is intended for advanced performance tuning. The default setting should be suitable for most users.innodb_doublewrite_pagesinnodb_doublewrite_pagesinnodb_doublewrite_pages变量控制每个线程的最大双写页面数。此变量用于高级性能调优。默认值对于大多数用户来说应当是合适的。Theinnodb_doublewrite_pagesvariable controls the maximum number of doublewrite pages per thread. This variable is intended for advanced performance tuning. The default value should be suitable for most users.
InnoDB 会自动加密属于加密表空间的双写文件页面(参见 第 17.13 节,“InnoDB 数据静态加密”)。同样,属于页面压缩表空间的双写文件页面会被压缩。因此,双写文件可能包含不同类型的页面,包括未加密和未压缩页面、加密页面、压缩页面,以及既加密又压缩的页面。InnoDB automatically encrypts doublewrite file
pages that belong to encrypted tablespaces (see
Section 17.13, “InnoDB Data-at-Rest Encryption”). Likewise, doublewrite
file pages belonging to page-compressed tablespaces are
compressed. As a result, doublewrite files can contain different
page types including unencrypted and uncompressed pages, encrypted
pages, compressed pages, and pages that are both encrypted and
compressed.