How to Permanently Set Ulimits for MongoDB (mongod) on CentOS/RHEL Systems


4 views

When running MongoDB in production environments, you'll often hit default system limits that need adjustment. The most critical ones for mongod are typically:

  • nproc (number of processes)
  • nofile (number of open files)
  • memlock (locked memory)

For CentOS/RHEL systems, the most reliable method is using /etc/security/limits.conf combined with a systemd service override (for CentOS 7+) or init script modification (for CentOS 6):

# /etc/security/limits.conf
mongod          soft    nofile          64000
mongod          hard    nofile          64000
mongod          soft    nproc           64000
mongod          hard    nproc           64000
mongod          soft    memlock         unlimited
mongod          hard    memlock         unlimited

Create an override file for the MongoDB service:

# /etc/systemd/system/mongod.service.d/override.conf
[Service]
LimitNOFILE=64000
LimitNPROC=64000
LimitMEMLOCK=infinity

Then reload and restart:

systemctl daemon-reload
systemctl restart mongod

For older systems using init scripts, modify /etc/init.d/mongod:

# Add near the top of the file
ulimit -n 64000
ulimit -u 64000
ulimit -l unlimited

After implementing changes, verify the limits are applied:

# Get mongod PID
pid=$(pgrep mongod)

# Check applied limits
cat /proc/$pid/limits

For systems using PAM, ensure the following is present in /etc/pam.d/login:

session    required   pam_limits.so

This guarantees limits are applied even for services started via sudo or other methods.


html

MongoDB requires specific ulimit settings to handle high concurrency and file operations efficiently. The most critical setting is nproc (user processes), which needs to be raised from typical defaults (often 1024) to at least 64000 for production MongoDB deployments.

For permanent ulimit changes that persist across reboots, we have several approaches:

Method 1: Using /etc/security/limits.conf

Edit the system-wide limits configuration:

# /etc/security/limits.conf
mongod       soft    nproc     64000
mongod       hard    nproc     64000
mongod       soft    nofile    64000
mongod       hard    nofile    64000
mongod       soft    fsize     unlimited
mongod       hard    fsize     unlimited

Method 2: Systemd Service Unit Modification (CentOS 7+)

Create or modify the MongoDB service unit file:

# /etc/systemd/system/mongod.service.d/limits.conf
[Service]
LimitNPROC=64000
LimitNOFILE=64000
LimitFSIZE=infinity

Then reload systemd:

systemctl daemon-reload
systemctl restart mongod

Method 3: SysVinit Configuration (CentOS 6)

For older systems using init scripts:

# /etc/init.d/mongod (add to start section)
ulimit -u 64000
ulimit -n 64000
ulimit -f unlimited

After making changes, verify the settings:

# Check process limits
cat /proc/$(pgrep mongod)/limits
# Check effective user limits
sudo -u mongod bash -c "ulimit -a"

Common issues and fixes:

  • Changes not applying: Ensure PAM is properly configured in /etc/pam.d/common-session
  • Permission issues: Check SELinux context if enforced: chcon -t mongod_var_lib_t /etc/security/limits.conf
  • System-wide defaults: Some systems override per-user limits in /etc/security/limits.d/90-nproc.conf

For mission-critical deployments:

# Recommended production settings (beyond minimums)
mongod       soft    nproc     65535
mongod       hard    nproc     65535
mongod       soft    nofile    65535
mongod       hard    nofile    65535
mongod       soft    memlock   unlimited
mongod       hard    memlock   unlimited