对innodb_flush_method的一点解释

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 官方文档描述如下: 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比较好

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
存储 SQL 缓存
MySQL `innodb_flush_log_at_trx_commit` 参数
MySQL `innodb_flush_log_at_trx_commit` 参数
|
存储 缓存 安全
【MySQL】innodb_flush_log_at_trx_commit=1,是干什么的?底层原理是什么?
【MySQL】innodb_flush_log_at_trx_commit=1,是干什么的?底层原理是什么?
160 0
|
存储 缓存 监控
【MySQL】innodb_log_buffer_size=4M,是干什么的?底层原理是什么?
【MySQL】innodb_log_buffer_size=4M,是干什么的?底层原理是什么?
120 0
|
缓存 关系型数据库 MySQL
提升mysql性能的关键参数之innodb_buffer_pool_size、innodb_buffer_pool_instances
提升mysql性能的关键参数之innodb_buffer_pool_size、innodb_buffer_pool_instances
1138 0
提升mysql性能的关键参数之innodb_buffer_pool_size、innodb_buffer_pool_instances
|
关系型数据库 分布式数据库 PolarDB
InnoDB buffer pool flush 策略
### InnoDB buffer pool flush 策略 **1. 刷脏整体策略** 首先从整体上来说, 刷脏的coordinator_thread 会判断进入哪一种场景刷脏 在 buf_flush_page_coordinator_thread() 函数里面 刷脏主要有3个场景 1. 如果 buf_flush_sync_lsn > 0, 则因为r
690 0
|
关系型数据库 MySQL 索引
|
缓存 关系型数据库 Linux

热门文章

最新文章