ulimit
Settingsulimit
设置On this page本页内容
Most UNIX-like operating systems, including Linux and macOS, provide ways to limit and control the usage of system resources such as threads, files, and network connections on a per-process and per-user basis. 大多数类UNIX操作系统(包括Linux和macOS)提供了按进程和按用户限制和控制系统资源(如线程、文件和网络连接)使用的方法。These "ulimits" prevent single users from using too many system resources. 这些“ulimits”防止单个用户使用过多的系统资源。Sometimes, these limits have low default values that can cause a number of issues in the course of normal MongoDB operation.有时,这些限制的默认值较低,在正常MongoDB操作过程中可能会导致许多问题。
mongod
and mongos
each use threads and file descriptors to track connections and manage internal operations. mongod
和mongos
各自使用线程和文件描述符来跟踪连接和管理内部操作。This section outlines the general resource utilization patterns for MongoDB. 本节概述了MongoDB的一般资源利用模式。Use these figures in combination with the actual information about your deployment and its use to determine ideal 将这些数字与有关部署及其使用的实际信息结合使用,以确定理想的ulimit
settings.ulimit
设置。
Generally, all 通常,所有mongod
and mongos
instances:mongod
和mongos
实例:
mongod
mongod
instance.mongod
实例使用的每个数据文件都有一个文件描述符。mongod
instance when storage.journal.enabled
is true
.storage.journal.enabled
为true
时,mongod
实例使用的每个日志文件的1个文件描述符。mongod
maintains a connection to all other members of the set.mongod
都保持与集合中所有其他成员的连接。mongod
uses background threads for a number of internal processes, including TTL collections, replication, and replica set health checks, which may require a small number of additional resources.将后台线程用于许多内部进程,包括TTL集合、复制和副本集运行状况检查,这可能需要少量额外资源。
mongos
In addition to the threads and file descriptors for client connections, 除了客户端连接的线程和文件描述符之外,mongos
must maintain connections to all config servers and all shards, which includes all members of all replica sets.mongos
还必须维护到所有配置服务器和所有分片的连接,其中包括所有副本集的所有成员。
For 对于mongos
, consider the following behaviors:mongos
,请考虑以下行为:
mongos
instances maintain a connection pool to each shard so that the mongos
can reuse connections and quickly fulfill requests without needing to create new connections.mongos
实例维护到每个分片的连接池,以便mongos
可以重用连接并快速完成请求,而无需创建新的连接。net.maxIncomingConnections
run-time option. net.maxIncomingConnections
运行时选项限制传入连接的数量。mongos
creates too many connections on the mongod
instances.mongos
在mongod
实例上创建过多连接的级联效应。ulimit
You can use the 您可以在系统提示符处使用ulimit
command at the system prompt to check system limits, as in the following example:ulimit
命令检查系统限制,如下例所示:
$ ulimit -a -t: cpu time (seconds) unlimited -f: file size (blocks) unlimited -d: data seg size (kbytes) unlimited -s: stack size (kbytes) 8192 -c: core file size (blocks) 0 -m: resident set size (kbytes) unlimited -u: processes 192276 -n: file descriptors 21000 -l: locked-in-memory size (kb) unlimited -v: address space (kb) unlimited -x: file locks unlimited -i: pending signals 192276 -q: bytes in POSIX msg queues 819200 -e: max nice 30 -r: max rt priority 65 -N 15: unlimited
ulimit
refers to the per-user limitations for various resources. 指的是各种资源的每用户限制。Therefore, if your 因此,如果您的mongod
instance executes as a user that is also running multiple processes, or multiple mongod
processes, you might see contention for these resources. mongod
实例作为同时运行多个进程或多个mongod
进程的用户执行,您可能会看到这些资源的争用。Also, be aware that the 此外,请注意processes
value (i.e. -u
) refers to the combined number of distinct processes and sub-process threads.processes
值(即-u)是指不同进程和子进程线程的组合数量。
On Linux, you can change 在Linux上,可以通过发出以下格式的命令来更改ulimit
settings by issuing a command in the following form:ulimit
设置:
ulimit -n <value>
There are both "hard" and the "soft" 影响MongoDB性能的有“硬”和“软”ulimit
s that affect MongoDB's performance. ulimit
。The "hard" “硬”ulimit
refers to the maximum number of processes that a user can have active at any time. ulimit
指的是用户在任何时候可以激活的最大进程数。This is the ceiling: no non-root process can increase the "hard" 这是上限:没有非根进程可以增加“硬”ulimit
. ulimit
。In contrast, the "soft" 相反,“软”ulimit
is the limit that is actually enforced for a session or process, but any process can increase it up to "hard" ulimit
maximum.ulimit
是会话或进程实际执行的限制,但任何进程都可以将其增加到“硬”ulimit
最大值。
A low "soft" 低“软”ulimit
can cause can't create new thread, closing connection
errors if the number of connections grows too high. ulimit
可能会导致can't create new thread, closing connection
错误,如果连接数增长过多。For this reason, it is extremely important to set both 因此,将两个ulimit
values to the recommended values.ulimit
值都设置为推荐值非常重要。
ulimit
will modify both "hard" and "soft" values unless the -H
or -S
modifiers are specified when modifying limit values.ulimit
将同时修改“硬”和“软”值,除非在修改极限值时指定了-H
或-S
修饰符。
For many distributions of Linux you can change values by substituting the 对于许多Linux发行版,可以通过将-n
option for any possible value in the output of ulimit -a
.ulimit -a
输出中的任何可能值替换为-n
选项来更改值。
After changing the 更改ulimit
settings, you must restart the process to take advantage of the modified settings. ulimit
设置后,必须重新启动进程以利用修改后的设置。On Linux, you can use the 在Linux上,可以使用/proc文件系统查看正在运行的进程的当前限制。/proc
file system to see the current limitations on a running process.
Depending on your system's configuration, and default settings, any change to system limits made using 根据系统的配置和默认设置,使用ulimit
may revert following a system restart. ulimit
对系统限制所做的任何更改都可能在系统重新启动后恢复。Check your distribution and operating system documentation for more information.有关详细信息,请查看发行版和操作系统文档。
You should typically start 通常应该使用mongod
using systemctl
, which uses the ulimit
settings:systemctl
启动mongod
,它使用ulimit设置:
systemctl start mongod.service
If you do not start 如果不使用mongod
using systemctl
, systemd
overrides some of the ulimit
settings. systemctl
启动mongod
,systemd
将覆盖一些ulimit
设置。For example, if you start 例如,如果启动mongod
as shown in the following command, then a user slice (such as user-1000.slice
) systemd
setting is used:mongod
,如以下命令所示,则使用用户分片(如user-1000.slice
)systemd
设置:
mongod --config ~/mongod.conf
A systemd
user slice limits the resources for the user's processes.systemd
用户分片限制用户进程的资源。
For macOS systems that have installed MongoDB Community using the brew installation method, the recommended open files value is automatically set when you start MongoDB through 对于使用brew安装方法安装MongoDB Community的macOS系统,当您通过brew services
.brew services
启动MongoDB时,会自动设置建议的打开文件值。
For macOS systems running MongoDb Enterprise or using the TGZ installation method, use the 对于运行MongoDb Enterprise或使用TGZ安装方法的macOS系统,请使用launchctl limit
command to set the recommended values. launchctl limit
命令设置建议值。See your operating system documentation for the precise procedure for changing system limits on running systems.有关在运行系统上更改系统限制的精确过程,请参阅操作系统文档。
Red Hat Enterprise Linux and CentOS 6 and 7 enforce a separate max process limitation, Red Hat Enterprise Linux和CentOS 6和7强制执行单独的最大进程限制nproc
, which overrides ulimit
settings. nproc
,该限制覆盖ulimit
设置。This value is defined in the following configuration file, depending on version:此值在以下配置文件中定义,具体取决于版本:
Version | File | |
---|---|---|
RHEL / CentOS 7 | 4096 | /etc/security/limits.d/20-nproc.conf |
RHEL / CentOS 6 | 1024 | /etc/security/limits.d/90-nproc.conf |
To configure an 要为这些版本配置nproc
value for these versions, create a file named /etc/security/limits.d/99-mongodb-nproc.conf
with new soft nproc
and hard nproc
values to increase the process limit. nproc
值,请创建一个名为/etc/security/limits.d/99-mongodb-nproc.conf
的文件,其中包含新的soft nproc
和hard nproc
,以增加进程限制。For recommended values, see Recommended 有关推荐值,请参阅推荐的ulimit
Settings.ulimit
设置。
With RHEL / CentOS 8, separate 对于RHEL/CCentOS 8,不再需要单独的nproc
values are no longer necessary. nproc
值。The ulimit
command is sufficient to configure the required max process values on RHEL / CentOS 8.ulimit
命令足以在RHEL/CCentOS 8上配置所需的最大进程值。
ulimit
Settingsulimit
设置Every deployment may have unique requirements and settings; however, the following thresholds and settings are particularly important for 每个部署可能有独特的要求和设置;然而,以下阈值和设置对于mongod
and mongos
deployments:mongod
和mongos
部署尤为重要:
-f
(file size): unlimited
-t
(cpu time): unlimited
-v
(virtual memory): unlimited
[1]-l
(locked-in-memory size): unlimited
-n
(open files): 128000
-m
(memory size): unlimited
[1] [2]-u
(processes/threads): 64000
For the macOS platform, the recommended process limit is 对于macOS平台,建议的进程限制为2500
, which is the maximum configurable value for this platform.2500
,这是该平台的最大可配置值。
Always remember to restart your 始终记得在更改ulimit设置后重新启动mongod
and mongos
instances after changing the ulimit
settings to ensure that the changes take effect.mongod
和mongos
实例,以确保更改生效。
For Linux distributions that use Upstart, you can specify limits within service scripts if you start 对于使用Upstart的Linux发行版,如果将mongod
and/or mongos
instances as Upstart services. mongod
和/或mongos
实例作为Upstart服务启动,则可以在服务脚本中指定限制。You can do this by using 您可以通过使用limit
stanzas.limit
节来实现这一点。
Specify the Recommended 指定推荐的ulimit
Settings, as in the following example:ulimit
设置,如下例所示:
limit fsize unlimited unlimited # (file size) limit cpu unlimited unlimited # (cpu time) limit as unlimited unlimited # (virtual memory size) limit memlock unlimited unlimited # (locked-in-memory size) limit nofile 64000 64000 # (open files) limit nproc 64000 64000 # (processes/threads)
Each 每个限制节将limit
stanza sets the "soft" limit to the first value specified and the "hard" limit to the second.limit
限制设置为指定的第一个值,将“硬”限制设置第二个值。
After changing 更改limit
stanzas, ensure that the changes take effect by restarting the application services, using the following form:limit
节后,请使用以下表单重新启动应用程序服务,以确保更改生效:
restart <service name>
systemd
systemd
的Linux发行版If you start a 如果将mongod
and/or mongos
instance as a systemd
service, you can specify limits within the [Service]
section of its service file. mongod
和/或mongos
实例作为systemd
服务启动,则可以在其服务文件的[Service]
部分中指定限制。The service file has a location like 服务文件的位置类似于/etc/systemd/system/<process-name>.service
./etc/systemd/system/<process-name>.service
。
You can set limits by using resource limit directives.可以使用资源限制指令设置限制。
Specify the Recommended 指定推荐的ulimit
Settings, as in the following example:ulimit
设置,如下例所示:
[Service] # Other directives omitted # (file size) LimitFSIZE=infinity # (cpu time) LimitCPU=infinity # (virtual memory size) LimitAS=infinity # (locked-in-memory size) LimitMEMLOCK=infinity # (open files) LimitNOFILE=64000 # (processes/threads) LimitNPROC=64000
Each 每个systemd
limit directive sets both the "hard" and "soft" limits to the value specified.systemd
限制指令都将“硬”和“软”限制设置为指定的值。
After changing 更改limit
stanzas, ensure that the changes take effect by restarting the application services, using the following form:limit
节后,请使用以下表单重新启动应用程序服务,以确保更改生效:
systemctl restart <service name>
If you installed MongoDB via a package manager such as 如果通过诸如yum
or apt
, the service file installed as part of your installation already contains these ulimit values.yum
或apt
之类的包管理器安装MongoDB,则作为安装的一部分安装的服务文件已经包含这些ulimit值。
/proc
This section applies only to Linux operating systems.本节仅适用于Linux操作系统。
The /proc
file-system stores the per-process limits in the file system object located at /proc/<pid>/limits
, where <pid>
is the process's PID or process identifier. /proc
文件系统将每个进程的限制存储在位于/proc/<pid>/limits
的文件系统对象中,其中<pid>
是进程的pid或进程标识符。You can use the following 您可以使用以下bash
function to return the content of the limits
object for a process or processes with a given name:bash
函数返回具有给定名称的一个或多个进程的limits
对象的内容:
return-limits(){ for process in $@; do process_pids=`ps -C $process -o pid --no-headers | cut -d " " -f 2` if [ -z $@ ]; then echo "[no $process running]" else for pid in $process_pids; do echo "[$process #$pid -- limits]" cat /proc/$pid/limits done fi done }
You can copy and paste this function into a current shell session or load it as part of a script. 您可以将此函数复制并粘贴到当前shell会话中,或将其作为脚本的一部分加载。Call the function with one the following invocations:使用以下调用之一调用函数:
return-limits mongod return-limits mongos return-limits mongod mongos
[1] | (1, 2) |
[2] | -m parameter to ulimit has no effect on Linux systems with kernel versions more recent than 2.4.30. ulimit 的-m 参数对内核版本高于2.4.30的Linux系统没有影响。-m if you wish.-m 。 |