MongoDB "too many files open" even after setting limits

When MongoDB starts up, I am greeted with a "too many files" error, even after editing /etc/security/limits.conf and setting the limit to unlimited.

Mar 09 18:29:13 ns524052 mongod[1298]: 2017-03-09T18:29:13.199+0100 I CONTROL  [initandlisten]     distarch: x86_64
Mar 09 18:29:13 ns524052 mongod[1298]: 2017-03-09T18:29:13.199+0100 I CONTROL  [initandlisten]     target_arch: x86_64
Mar 09 18:29:13 ns524052 mongod[1298]: 2017-03-09T18:29:13.199+0100 I CONTROL  [initandlisten] options: { net: { port: 29000 }, security: { authorization: "enabled" }, storage: { dbPath: "/home/databases/mongo" }, systemLog: { quiet: true } }
Mar 09 18:29:13 ns524052 mongod[1298]: 2017-03-09T18:29:13.235+0100 I -        [initandlisten] Detected data files in /home/databases/mongo created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
Mar 09 18:29:13 ns524052 mongod[1298]: 2017-03-09T18:29:13.236+0100 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=37G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
Mar 09 18:29:15 ns524052 mongod[1298]: 2017-03-09T18:29:15.676+0100 E STORAGE  [initandlisten] WiredTiger (24) [1489080555:676881][1298:0x70e199ff3c80], file:collection-160--541918095290639536.wt, WT_SESSION.open_cursor: /home/databases/mongo/collection-160--541918095290639536.wt: handle-open: open: Too many open files
Mar 09 18:29:15 ns524052 mongod[1298]: 2017-03-09T18:29:15.676+0100 I -        [initandlisten] Invariant failure: ret resulted in status UnknownError: 24: Too many open files at src/mongo/db/storage/wiredtiger/wiredtiger_session_cache.cpp 79

My limits.conf contains this

*                soft    nofile          unlimited
*                hard    nofile          unlimited
*                soft    nproc           unlimited
*                hard    nproc           unlimited

I've also tried using ulimit with no luck. Not sure what's happened. Running on Ubuntu 16.04.


Since Ubuntu 16.04 uses systemd, you have to adapt the ulimit settings on a per service basis. To do so, create a file /etc/systemd/system/mongodb.service.d/override.conf and override the defaults.

root@xenial:~# sudo systemctl edit mongodb.service

Paste them:

[Service]
LimitNOFILE=infinity
LimitNPROC=infinity

Ctrl + O then Ctrl + X to exit and file /etc/systemd/system/mongodb.service.d/override.conf is created

root@xenial:~# cat /etc/systemd/system/mongodb.service.d/override.conf
[Service]
LimitNOFILE=infinity
LimitNPROC=infinity

To check if these settings were applied, you can use systemctl show. First, let's see the values that are active.

root@xenial:~# systemctl --no-pager show mongodb.service | egrep 'NOFILE|NPROC'
LimitNOFILE=1024
LimitNOFILESoft=1024
LimitNPROC=7839
LimitNPROCSoft=7839

Then apply the settings.

root@xenial:~# systemctl daemon-reload 
root@xenial:~# systemctl --no-pager show mongodb.service | egrep 'NOFILE|NPROC'
LimitNOFILE=18446744073709551615
LimitNOFILESoft=18446744073709551615
LimitNPROC=18446744073709551615
LimitNPROCSoft=18446744073709551615