UNIX ulimit SettingsUNIX ulimit设置

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操作过程中可能会导致许多问题。

Resource Utilization资源利用率

mongod and mongos each use threads and file descriptors to track connections and manage internal operations. mongodmongos各自使用线程和文件描述符来跟踪连接和管理内部操作。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:通常,所有mongodmongos实例:

  • track each incoming connection with a file descriptor and a thread.使用文件描述符和线程跟踪每个传入连接。
  • track each internal thread or pthread as a system process.作为系统进程跟踪每个内部线程或pthread

mongod

  • 1 file descriptor for each data file in use by the mongod instance.mongod实例使用的每个数据文件都有一个文件描述符。
  • 1 file descriptor for each journal file used by the mongod instance when storage.journal.enabled is true.storage.journal.enabledtrue时,mongod实例使用的每个日志文件的1个文件描述符。
  • In replica sets, each 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可以重用连接并快速完成请求,而无需创建新的连接。
  • You can limit the number of incoming connections using the net.maxIncomingConnections run-time option. 可以使用net.maxIncomingConnections运行时选项限制传入连接的数量。By restricting the number of incoming connections you can prevent a cascade effect where the mongos creates too many connections on the mongod instances.通过限制传入连接的数量,可以防止mongosmongod实例上创建过多连接的级联效应。

Review and Set Resource Limits查看和设置资源限制

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 ulimit settings by issuing a command in the following form:在Linux上,可以通过发出以下格式的命令来更改ulimit设置:

ulimit -n <value>

There are both "hard" and the "soft" ulimit s that affect MongoDB's performance. 影响MongoDB性能的有“硬”和“软”ulimitThe "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. 这是上限:没有非根进程可以增加“硬”ulimitIn 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 -n option for any possible value in the output of ulimit -a.对于许多Linux发行版,可以通过将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 /proc file system to see the current limitations on a running process.在Linux上,可以使用/proc文件系统查看正在运行的进程的当前限制。

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启动mongodsystemd将覆盖一些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.slicesystemd设置:

mongod --config ~/mongod.conf
Note注意

A systemd user slice limits the resources for the user's processes.systemd用户分片限制用户进程的资源。

macOS

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 services.对于使用brew安装方法安装MongoDB Community的macOS系统,当您通过brew services启动MongoDB时,会自动设置建议的打开文件值。

For macOS systems running MongoDb Enterprise or using the TGZ installation method, use the launchctl limit command to set the recommended values. 对于运行MongoDb Enterprise或使用TGZ安装方法的macOS系统,请使用launchctl limit命令设置建议值See your operating system documentation for the precise procedure for changing system limits on running systems.有关在运行系统上更改系统限制的精确过程,请参阅操作系统文档。

Red Hat Linux Enterprise Server and CentOSRed Hat Linux Enterprise Server和CentOS

Red Hat Enterprise Linux and CentOS 6 and 7 enforce a separate max process limitation, nproc, which overrides ulimit settings. Red Hat Enterprise Linux和CentOS 6和7强制执行单独的最大进程限制nproc,该限制覆盖ulimit设置。This value is defined in the following configuration file, depending on version:此值在以下配置文件中定义,具体取决于版本:

VersionValueFile
RHEL / CentOS 74096/etc/security/limits.d/20-nproc.conf
RHEL / CentOS 61024/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 nprochard nproc,以增加进程限制。For recommended values, see Recommended ulimit Settings.有关推荐值,请参阅推荐的ulimit设置

With RHEL / CentOS 8, separate nproc values are no longer necessary. 对于RHEL/CCentOS 8,不再需要单独的nproc值。The ulimit command is sufficient to configure the required max process values on RHEL / CentOS 8.ulimit命令足以在RHEL/CCentOS 8上配置所需的最大进程值。

Recommended ulimit Settings推荐的ulimit设置

Every deployment may have unique requirements and settings; however, the following thresholds and settings are particularly important for mongod and mongos deployments:每个部署可能有独特的要求和设置;然而,以下阈值和设置对于mongodmongos部署尤为重要:

  • -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
Note注意

For the macOS platform, the recommended process limit is 2500, which is the maximum configurable value for this platform.对于macOS平台,建议的进程限制为2500,这是该平台的最大可配置值。

Always remember to restart your mongod and mongos instances after changing the ulimit settings to ensure that the changes take effect.始终记得在更改ulimit设置后重新启动mongodmongos实例,以确保更改生效。

Linux distributions using Upstart使用Upstart的Linux发行版

For Linux distributions that use Upstart, you can specify limits within service scripts if you start mongod and/or mongos instances as Upstart services. 对于使用Upstart的Linux发行版,如果将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>

Linux distributions using 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>
Note注意

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.如果通过诸如yumapt之类的包管理器安装MongoDB,则作为安装的一部分安装的服务文件已经包含这些ulimit值。

/proc File System文件系统

Note注意

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) If you limit virtual or resident memory size on a system running MongoDB the operating system will refuse to honor additional allocation requests.如果在运行MongoDB的系统上限制虚拟或驻留内存大小,操作系统将拒绝接受额外的分配请求。
[2] The -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系统没有影响。You may omit -m if you wish.如果你愿意,可以省略-m
←  Disable Transparent Huge Pages (THP)Configuration and Maintenance →