{{ it.name }}
{{ it.text }}
一、起因
ulimit -n
查看)实际即文件描述符(fd
)。
根据配置(配置的 3 个参数值或默认值)计算 request_open_files(需要的文件描述符)
获取有效的系统的限制值 effective_open_files
根据 effective_open_files 调整 request_open_files
根据调整后的 request_open_files,计算实际生效的参数值(show variables
查看到的 3 个参数值)
# 最大连接数+同时打开的表的最大数量+其他(各种日志等等)
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);
limit = requested_open_files - 10 - TABLE_OPEN_CACHE_MIN * 2;
如果配置的 max_connections 值大于 limit,则将 max_connections 的值修正为 limit
# 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);
- 系统资源不够且无法调整
# 参数设置
mysql max_connections = 1000 //ulimit -n 1024
# 生效的值
open_files_limit = 1024 max_connections = 1024 - 10 - 800 = 214 table_open_cache = ( 1024 - 10 - 214) / 2 = 400
---
- 系统资源不够可以调整
# 参数设置
mysql max_connections = 1000 //ulimit -S -n 1000 //ulimit -H -n 65535
# 生效的值
open_files_limit = 65535 max_connections = 1000 table_open_cache = ( 1024 - 10 - 214) / 2 = 400
---
- mysql 修改 open_files_limit
# 参数设置
//mysql max_connections = 1000 max_connections = 1000 //ulimit -n 65535
# 生效的值
open_files_limit = 65535 max_connections = 1000 table_open_cache = 2000 ```
淘宝数据库内核月报中说道的相关内容:《 MySQL·答疑解惑·open file limits 》这篇主要讲的是,MySQL 在执行哪些操作时会执行打开文件的操作。