时间:2021-07-01 10:21:17 帮助过:30人阅读
目录
非root用户运行MySQL,当MySQL配置比较高时,MySQL运行中生效的参数值与配置的值不一样。
这篇文章的目的是为了说明在系统资源不够的情况下,MySQL 是怎么调整以下三个参数的: open_files_limit、max_connections、 table_open_cache。
此文涉及到三个参数open_files_limit、 max_connections、 table_open_cache。与这三个参数相关的系统资源是打开文件数限制,即文件描述符(fd)限制。
系统参数与文件描述符的关系
– max_connection & fd : 每一个MySQL connection都需要一个文件描述符
– table_open_cache & fd: 打开一张表至少需要一个文件描述符,如打开MyISAM需要两个fd
– 系统的打开文件数限制可以通过 ulimit -n查看
request_open_files(需要的文件描述符)effective_open_fileseffective_open_files调整request_open_filesrequest_open_files,计算实际生效的参数值(show variables 可查看参数值)request_open_filesrequest_open_filesrequest_open_files有三个计算条件
// 最大连接数+同时打开的表的最大数量+其他(各种日志等等)
limit_1= max_connections + table_cache_size * 2 + 10;
//假设平均每个连接打开的表的数量(2-4)
//源码中是这么写的:
//We are trying to allocate no less than
// max_connections*5 file handles
limit_2= max_connections * 5;
//mysql 默认的默认是5000
limit_3= open_files_limit ? open_files_limit : 5000;
所以open_files_limit期待的最低
request_open_files= max(limit_1, limit_2,limit_3);
effective_open_filesMySQL 的思路:
在有限值的的范围内MySQL 尽量将effective_open_files的值设大

request_open_filesrequested_open_files= min(effective_open_files, request_open_files);
open_files_limitopen_files_limit = effective_open_files
max_connectionsmax_connections 根据 request_open_files 来做修正。
limit = requested_open_files - 10 - TABLE_OPEN_CACHE_MIN * 2;
max_connections值大于limit,则将 max_connections 的值修正为limitmax_connections 保留配置值table_cache_sizetable_cache_size 会根据 request_open_files 来做修正
// mysql table_cache_size 最小值,400
limit1 = TABLE_OPEN_CACHE_MIN
// 根据 requested_open_files 计算
limit2 = (requested_open_files - 10 - max_connections) / 2
limit = max(limit1,limt2);
table_cache_size 值大于limit,则将 table_cache_size 的值修正为limittable_cache_size 保留配置值以下用例在非 root 用户下运行
参数设置:
//mysql
max_connections = 500
table_open_cache = 999
//ulimit -n
1500
生效的值:
open_files_limit = 1500
max_connections = min[(1500 - 10 - 800),500] = 500
table_open_cache = ( 1500 - 10 - 500) / 2 = 495
在Linux最大打开文件数限制下 MySQL 对参数的调整
标签:参数 tle root 最小 cat ulimit min ble amp