5

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.

inventor02
  • 53
  • 1
  • 3

2 Answers2

4

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
ZNApps
  • 3
  • 1
Thomas
  • 4,155
  • 5
  • 21
  • 28
  • This caused mongo to completely fail for me with the error: `Failed to restart mongodb.service: Unit mongodb.service is not loaded properly: Invalid argument.` Only removing that junk service file and then re-running daemon-reload fixed it. – Cerin Feb 06 '18 at 18:31
  • Then there seems something wrong with the service file. It might be better to open another question and put all the information including your service file in there. – Thomas Feb 06 '18 at 19:27
  • `cat: /etc/systemd/system/mongodb.service: No such file or directory` Did you test this on Ubuntu 16? The mongo-org package does not create any such file. – Cerin Feb 07 '18 at 19:48
  • You can't just create that file anew. You have to copy and alter the original. _cp /lib/systemd/system/mongodb.service /etc/systemd/system/_ and then you can edit the new file _vim /etc/systemd/system/mongodb.service_ and add those 2 new lines under the [Service] section. Then just stop & start mongodb. – natli Mar 15 '18 at 15:09
  • @natli: you are right, that should go into the `override.conf` file. Updated answer and wondering how this could be accepted and upvoted. =0) – Thomas Mar 15 '18 at 17:05
  • Thomas' solution worked for me, though the service file is `/etc/systemd/system/mongod.service` on Ubuntu 16.04 for MongoDB 3.6.6. So the service override command changes to: sudo systemctl edit mongod.service – Ravi Misra Jun 19 '19 at 11:19
0

Apart from user limits there might be opened files limit (max opened file descriptors limit to be precise) set in kernel:

# sysctl -a |grep -i fs.file-max

adjust it with

# sysctl -w fs.file-max=<opened_file_new_limit>

To keep kernel setting survive reboots - add file to /etc/sysctl.d directory, file should have .conf extension and content should be as following:

fs.file-max=<opened_file_new_limit>

Also check if there are no other files in /etc/sysctl.d directory that could overwrite these settings - there's another file when this can be set: /etc/sysctl.conf (if files from /etc/systcl.d are not read then use /etc/sysctl.conf)

That resolved issue for me.

sysctl config documentation: https://www.freedesktop.org/software/systemd/man/sysctl.d.html

valdek
  • 11
  • 4