文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 92%

17.6.4 双写缓冲区17.6.4 Doublewrite Buffer

双写缓冲区是一个存储区域,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 变量控制双写缓冲区是否启用。在大多数情况下,它默认是启用的。要禁用双写缓冲区,将 innodb_doublewrite 设置为 OFF。如果您更关心性能而非数据完整性,例如在进行基准测试时,可以考虑禁用双写缓冲区。The innodb_doublewrite variable controls whether the doublewrite buffer is enabled. It is enabled by default in most cases. To disable the doublewrite buffer, set innodb_doublewrite to OFF. 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_doublewrite supports DETECT_AND_RECOVER and DETECT_ONLY settings.

    DETECT_AND_RECOVER 设置与 ON 设置相同。使用此设置时,双写缓冲区将完全启用,数据库页面内容会写入双写缓冲区,在恢复过程中访问该缓冲区以修复未完成的页面写入。The DETECT_AND_RECOVER setting is the same as the ON setting. 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 the DETECT_ONLY setting, 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 the innodb_doublewrite setting that enables the doublewrite buffer, between ON, DETECT_AND_RECOVER, and DETECT_ONLY. MySQL does not support dynamic changes between a setting that enables the doublewrite buffer and OFF or 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 the innodb_doublewrite setting 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, an innodb_flush_method setting of O_DIRECT is recommended.

  • innodb_doublewrite_dir

    innodb_doublewrite_dir 变量定义了 InnoDB 创建双写文件的目录。如果未指定目录,双写文件将创建在 innodb_data_home_dir 目录中,如果未指定则默认为数据目录。The innodb_doublewrite_dir variable defines the directory where InnoDB creates doublewrite files. If no directory is specified, doublewrite files are created in the innodb_data_home_dir directory, 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_files

    innodb_doublewrite_files 变量定义了 doublewrite 文件的数量,默认为 2。默认情况下,每个缓冲池实例会创建两个 doublewrite 文件:一个用于 flush 列表的 doublewrite 文件和一个用于 LRU 列表的 doublewrite 文件。The innodb_doublewrite_files variable 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 the InnoDB page 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 the InnoDB page 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 the innodb_buffer_pool_instances variable.)

    双写文件名具有以下格式:#ib_page_size_file_number.dblwr(在 DETECT_ONLY 设置下为 .bdblwr)。例如,对于具有 16KB InnoDB 页面大小和单个缓冲池的 MySQL 实例,将创建以下双写文件:Doublewrite file names have the following format: #ib_page_size_file_number.dblwr (or .bdblwr with the DETECT_ONLY setting). For example, the following doublewrite files are created for a MySQL instance with an InnoDB pages size of 16KB and a single buffer pool:

    #ib_16384_0.dblwr
    #ib_16384_1.dblwr
    

    innodb_doublewrite_files 变量用于高级性能调整。默认设置对于大多数用户来说应该是合适的。The innodb_doublewrite_files variable is intended for advanced performance tuning. The default setting should be suitable for most users.

  • innodb_doublewrite_pagesinnodb_doublewrite_pages

    innodb_doublewrite_pages 变量控制每个线程的最大双写页面数。此变量用于高级性能调优。默认值对于大多数用户来说应当是合适的。The innodb_doublewrite_pages variable 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.