51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

频繁使用MySQL,磁盘IO高峰或爆满,innodb_buffer_pool_size解决方案

原因:最近项目优化过程中,偶然发现本地服务器每过10s就会有一次磁盘爆满的现象。在我们的项目中发现本地项目中存在一个用作"同步数据"的定时任务每过10s就查询数据库表,并把这些信息发送HTTP请求。

方案 :频繁的数据库请求导致本地的服务器每过10s就产生一次高峰。在优化这个定时任务前,了解到MySQL中的innodb_buffer_pool_size参数,这个参数用来设置Innodb缓冲池大小且默认值为128M。

查看了服务器上的MySQL的innodb_buffer_pool_size参数,大小居然只有8M。。。

修改

直接说结论,innodb_buffer_pool_size的值官方建议在32位机器下设置为2-3.5G。我们的服务器是16G,本次设置成了3G,就已经解决了磁盘爆满问题。(数据量没有非常大)

下面是官方关于innodb_buffer_pool_size属性的原文:

InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes androw data. The bigger you set this the less disk I/O is needed to access data in tables. On a dedicated database server you may set this parameter up to 80% of the machine physical memory size. Do not set it too large, though, because competition of the physical memory may cause paging in the operating system. Note that on 32bit systems you might be limited to 2-3.5G of user level memory per process, so do not set it too high.

方式一、临时有效(本次启动)

  • 进入cmd,输入mysql -uroot -p,输入密码登录MySQL。查看原先缓冲池大小:
    show variables like 'innodb_buffer_pool_size';


  • 输入 set global innodb_buffer_pool_size=缓存池大小。
    这里的单位是B,所以1M是1024 * 1024 * 1 = 1,048,576,1G是1024 * 1024 * 1024 * 1 = 1,073,741,824。如果需要3G空间就是1024 * 1024 * 1024 * 3 = 3221225472

注意:

  • 如果新值小于my.ini中的值,这里不会设置成功。

  • 值不是innodb_buffer_pool_instances 或者innodb_buffer_pool_chunk_size 倍数时,会自动补正。(下面会介绍这两个参数)

方式二、永久生效

修改C:\ProgramData\MySQL\MySQL Server 8.0下的my.ini文件。注意ProgramData是隐藏的。

  • 注意文件的编码格式,用记事本打开写可能变成utf-8格式,在网上看见有人说这里用utf-8可能会出现问题,保险起见还是ANSI格式。

拓展

这里还需要注意

innodb_buffer_pool_instances

innodb_buffer_pool_instances 定义 InnoDB缓冲池的实例数,也就是将innodb_buffer_pool_size 分成几个实例,官方给出的解释中,这个参数的作用为,
innodb_buffer_pool_instances 对于缓冲池在数千兆字节范围内的系统,通过减少争用不同线程对缓存页面进行读写的争用,将缓冲池划分为多个单独的实例可以提高并发性。设置缓冲池大小innodb_buffer_pool_size为1G以上时,默认值是8;1G以下时,默认值是1。

The number of regions that the InnoDB buffer pool is divided into. For systems with buffer pools in the multi-gigabyte range, dividing the buffer pool into separate instances can improve concurrency, by reducing contention as different threads read and write to cached pages.

innodb_buffer_pool_chunk_size

用来定义InnoDB缓冲池大小调整操作的块大小,my.ini文件中并没有此项配置,只能在运行时通过show variables like 'innodb_buffer_pool_chunk_size' 查看,并在运行时通过set global innodb_buffer_pool_chunk_size=块大小 来设置。
官方中的解释为 innodb_buffer_pool_size / innodb_buffer_pool_chunk_size 不应该大于1000。


赞(6)
未经允许不得转载:工具盒子 » 频繁使用MySQL,磁盘IO高峰或爆满,innodb_buffer_pool_size解决方案