对innodb_flush_method的一点解释

简介: 官方文档描述如下: By default, InnoDB uses the fsync()system call to flush both the data and log files.
官方文档描述如下:


By default, InnoDB uses the fsync()system call to flush both the data and log files. If
innodb_flush_method option is set to O_DSYNC, InnoDB uses O_SYNC to open and flush the
log files, and fsync()to flush the data files. If O_DIRECT is specified (available on some GNU/
Linux versions, FreeBSD, and Solaris), InnoDBuses O_DIRECT(or directio()on Solaris) to
open the data files, and uses fsync()to flush both the data and log files. Note that InnoDB uses
fsync() instead of fdatasync(), and it does not use O_DSYNC by default because there have
been problems with it on many varieties of Unix.


innodb_flush_method
常规3个值
1、fdatasync
2、O_DSYNC
3、O_DIRECT


正常的访问方式 
用户态缓存---》内核态缓存---》磁盘


按照MYSQL的描述
1、fdatasync
InnoDB uses the fsync() system call to flush both the data and log files.
Note that InnoDB uses fsync() instead of fdatasync()
虽然MYSQL可以使用fdatasync为参数但是实际上是调用的系统的 fsync()函数,
我们可以看看LINUX下FSYNC()函数的描述
fsync()  transfers  ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the
       file descriptor fd to the disk device (or other permanent storage device) so that all changed information can be retrieved  even
       after  the  system crashed or was rebooted.  This includes writing through or flushing a disk cache if present.  The call blocks
       until the device reports that the transfer has completed.  It also flushes metadata information associated with  the  file  (see
       stat(2)).
       简单的说这个参数用于同步所有线程修改过的文件,而进程中的PCB中记录了打开文件,他是通过文件描述符进行匹配的
       在LINUX内核中/usr/src/linux-headers-3.19.0-25-generic/include/linux/sched.h
       有如下的PCB结构体
       struct task_struct { }
       其中有一个files_struct的结构体,而文件描述符就是这样一个结构体的指针
       那么只要MYSQL线程进行了刷新动作,那么他的这些文件的数据一定会同步到磁盘
2、O_DSYNC
InnoDB uses O_SYNC to open and flush the log files, and fsync()to flush the data files
当设置为这个选项的时候,当MYSQL线程打开LOGFILE的时候使用的O_SYNC的方式,而对数据文件还是使用的fsync()
我们知道对一个文件进行读,打开是使用LINUX的
open()函数,而其中也就有这样一个选项
O_SYNC The  file  is  opened for synchronous I/O.  Any write(2)s on the resulting file descriptor will block the calling process
       until the data has been physically written to the underlying hardware.
       他描述的是如果这样打开一个文件那么在数据从内核态缓存写到了物理磁盘前,任何试图修改文件描述符的进程都会被堵塞。如此保证了日志文件
       最大的安全性
3、O_DIRECT
If O_DIRECT is specified (available on some GNU/Linux versions, FreeBSD, and Solaris), InnoDB 
uses O_DIRECT(or directio()on Solaris) to open the data files, and uses 
fsync()to flush both the data and log files.
使用这个选项MYSQL使用O_DIRECT方式打开数据文件,而fsync()会最终同步所有的数据和日志文件。
我们同样看看open()函数中关于O_DIRECT描述
O_DIRECT (Since Linux 2.4.10)
         Try to minimize cache effects of the I/O to and from this file.  In general this will degrade performance, but it is use‐
         ful  in special situations, such as when applications do their own caching.  File I/O is done directly to/from user-space
         buffers.  The O_DIRECT flag on its own makes an effort to transfer data synchronously, but does not give  the  guarantees
         of  the  O_SYNC flag that data and necessary metadata are transferred.  To guarantee synchronous I/O, O_SYNC must be used
         in addition to O_DIRECT.  
         使用这个选项一般来说会降低性能,但是在特定的情况下比如应用程序有自己的缓存机制。I/O直接来自用户态的缓存,O_DIRECT标识对大量的
         数据写有利,因为他绕开了内核态缓存,但是他并同步METADATA(这里占时理解为INODE缓存,也就是文件的基本信息比如打开时间修改时间等)
         所以要完成同步必须同时调用O_SYNC。
         如此我们也了解为什么为什么使用O_DIRECT还会调用FSYNC()我们知道FSYNC()是全同步的,LINUX上的传统的FDATASYNC()是不同步METADATA的
         如INODE缓存,进程描述缓存。但是他能够对大量的数据绕开缓存,提高性能,需要最后同步的只是DATAFILE的METADAT。

        MYSQL有自己的缓冲,这种可以使用 O_DIRECT比较好

水平有限,如有错误请指出

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
数据建模 Linux 数据库
简单实用的数据建模工具PDManer
PDManer是一款开源的国产数据建模工具
16110 1
简单实用的数据建模工具PDManer
|
人工智能 并行计算 调度
AI创业公司的算力困境,远比你想象的更复杂
当前AI创业公司面临严峻“算力困局”:不仅受制于高昂成本,更受限于技术封锁、生态绑定与资源低效。算力获取难、用不起、用不好,正成为制约创新的关键瓶颈。
|
存储 缓存 监控
MySQL服务器配置优化:my.cnf参数调优指南
本文深入解析了MySQL核心配置参数及性能优化技巧,涵盖内存结构、调优原则、存储引擎优化、查询性能优化等内容,通过实战案例帮助读者构建高性能MySQL服务器配置,解决常见的性能瓶颈问题。
1633 0
|
机器学习/深度学习 监控 安全
IDS 和 IPS 的区别详解
【8月更文挑战第31天】
2423 0
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17594 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置

热门文章

最新文章