作者:魏新平,知数堂第5期MySQL实战班学员,第10期MySQL优化班学员,现任职助教。
文章来源:知数堂


一、官方解释
1) 10 + maxconnections + (tableopen_cache * 2)
2) max_connections * 5
3) operating system limit if positive
4) if operating system limit is Infinity:
open_files_limit value specified at startup, 5000 if none
二、测试mysql版本和系统版本
没有配置open_files_limit参数(root用户登陆操作)
[mysqld]
user = mysql
port = 5721
socket = tmp/mysql_sandbox5721.sock
basedir = root/opt/mysql/5.7.21
datadir = opt/msb_5_7_21/data
tmpdir = /opt/msb_5_7_21/tmp
pid-file = /opt/msb_5_7_21/data/mysql_sandbox5721.pid
bind-address = 127.0.0.1
[root@mysqlmaster ~]# ps -ef | grep mysqld
root 30471009:52 pts/200:00:00/bin/sh bin/mysqld_safe --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf
mysql 32533047009:52 pts/200:00:02/root/opt/mysql/5.7.21/bin/mysqld --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf --basedir=/root/opt/mysql/5.7.21--datadir=/opt/msb_5_7_21/data --plugin-dir=/root/opt/mysql/5.7.21/lib/mysql/plugin --user=mysql --log-error=/opt/msb_5_7_21/data/msandbox.err --pid-file=/opt/msb_5_7_21/data/mysql_sandbox5721.pid --socket=/tmp/mysql_sandbox5721.sock --port=5721
root 105835177009:57 pts/400:00:00 grep --color=auto mysqld
[root@mysqlmaster ~]# cat /proc/3253/limits |grep files
Max open files 50005000 files
--------------------------------------------------------------------------------
mysql [localhost:5721] {root} ((none)) > SELECT @@open_files_limit;
+--------------------+
| @@open_files_limit|
+--------------------+
| 5000|
+--------------------+
1 row inset(0.00 sec)
4) if operating system limit is Infinity:
openfileslimit value specified at startup, 5000 if none
10 + maxconnections + (tableopen_cache * 2)最大
[root@mysqlmaster msb_5_7_21]# ps -ef | grep mysqld
root 800091010:43 pts/200:00:00/bin/sh bin/mysqld_safe --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf
mysql 80227800091110:43 pts/200:00:01/root/opt/mysql/5.7.21/bin/mysqld --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf --basedir=/root/opt/mysql/5.7.21--datadir=/opt/msb_5_7_21/data --plugin-dir=/root/opt/mysql/5.7.21/lib/mysql/plugin --user=mysql --log-error=/opt/msb_5_7_21/data/msandbox.err --pid-file=/opt/msb_5_7_21/data/mysql_sandbox5721.pid --socket=/tmp/mysql_sandbox5721.sock --port=5721
root 805551129010:43 pts/200:00:00 grep --color=auto mysqld
[root@mysqlmaster msb_5_7_21]#
[root@mysqlmaster msb_5_7_21]# cat /proc/80227/limits |grep files
Max open files 61626162 files
------------------------------------------------------------------------------------------------
mysql [localhost:5721] {root} ((none)) > SELECT @@open_files_limit;
+--------------------+
| @@open_files_limit|
+--------------------+
| 6162|
+--------------------+
1 row inset(0.00 sec)
max_connections * 5最大
[root@mysqlmaster msb_5_7_21]# ps -ef | grep mysqld
root 879141010:48 pts/200:00:00/bin/sh bin/mysqld_safe --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf
mysql 88132879142110:48 pts/200:00:01/root/opt/mysql/5.7.21/bin/mysqld --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf --basedir=/root/opt/mysql/5.7.21--datadir=/opt/msb_5_7_21/data --plugin-dir=/root/opt/mysql/5.7.21/lib/mysql/plugin --user=mysql --log-error=/opt/msb_5_7_21/data/msandbox.err --pid-file=/opt/msb_5_7_21/data/mysql_sandbox5721.pid --socket=/tmp/mysql_sandbox5721.sock --port=5721
root 883001129010:48 pts/200:00:00 grep --color=auto mysqld
[root@mysqlmaster msb_5_7_21]# cat /proc/88132/limits | grep files
Max open files 1000510005 files
----------------------------------------------------------------------------------------------
mysql [localhost:5721] {root} ((none)) > SELECT @@open_files_limit;
+--------------------+
| @@open_files_limit|
+--------------------+
| 10005|
+--------------------+
1 row inset(0.00 sec)
operating system limit if positive最大
[root@mysqlmaster msb_5_7_21]# ps -ef | grep mysql
root 938251010:52 pts/200:00:00/bin/sh bin/mysqld_safe --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf
mysql 94031938251610:52 pts/200:00:01/root/opt/mysql/5.7.21/bin/mysqld --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf --basedir=/root/opt/mysql/5.7.21--datadir=/opt/msb_5_7_21/data --plugin-dir=/root/opt/mysql/5.7.21/lib/mysql/plugin --user=mysql --log-error=/opt/msb_5_7_21/data/msandbox.err --pid-file=/opt/msb_5_7_21/data/mysql_sandbox5721.pid --socket=/tmp/mysql_sandbox5721.sock --port=5721
root 942541129010:52 pts/200:00:00 grep --color=auto mysql
[root@mysqlmaster msb_5_7_21]# cat /proc/94031/limits | grep files
Max open files 60006000 files
---------------------------------------------------------------------------------------
mysql [localhost:5721] {root} ((none)) > SELECT @@open_files_limit;
+--------------------+
| @@open_files_limit|
+--------------------+
| 6000|
+--------------------+
1 row inset(0.00 sec)
三、配置open_files_limit参数(root用户登陆操作)
[root@mysqlmaster msb_5_7_21]# ps -ef | grep mysqld
root 1040481010:58 pts/200:00:00/bin/sh bin/mysqld_safe --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf
mysql 1042691040481910:58 pts/200:00:01/root/opt/mysql/5.7.21/bin/mysqld --defaults-file=/opt/msb_5_7_21/my.sandbox.cnf --basedir=/root/opt/mysql/5.7.21--datadir=/opt/msb_5_7_21/data --plugin-dir=/root/opt/mysql/5.7.21/lib/mysql/plugin --user=mysql --log-error=/opt/msb_5_7_21/data/msandbox.err --open-files-limit=4200--pid-file=/opt/msb_5_7_21/data/mysql_sandbox5721.pid --socket=/tmp/mysql_sandbox5721.sock --port=5721
root 1044601129010:58 pts/200:00:00 grep --color=auto mysqld
[root@mysqlmaster msb_5_7_21]# cat /proc/104269/limits | grep files
Max open files 42004200 files
------------------------------------------------------------------
mysql [localhost:5721] {root} ((none)) > SELECT @@open_files_limit;
+--------------------+
| @@open_files_limit|
+--------------------+
| 4200|
+--------------------+
1 row inset(0.00 sec)
四、总结
1) 10 + maxconnections + (tableopen_cache * 2)
2) max_connections * 5
3) operating system limit if positive
4) if operating system limit is Infinity:
open_files_limit value specified at startup, 5000 if none
相关链接
MySQL行级别并行复制能并行应用多少个binlog group?
MySQL高可用工具Orchestrator系列四:拓扑恢复
MySQL高可用工具Orchestrator系列三:探测机制
MySQL高可用工具Orchestrator系列二:复制拓扑的发现
MySQL高可用工具Orchestrator系列一:单节点模式安装
Oracle RAC Cache Fusion系列十八:Oracle RAC Statisticsand Wait Events

更多干货,欢迎来撩~




