即使设置了限制,MongoDB 也“打开了太多文件”

inv*_*r02 5 mongodb ubuntu-16.04

当 MongoDB 启动时,即使在编辑/etc/security/limits.conf并将限制设置为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
Run Code Online (Sandbox Code Playgroud)

我的limits.conf包含这个

*                soft    nofile          unlimited
*                hard    nofile          unlimited
*                soft    nproc           unlimited
*                hard    nproc           unlimited
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用但ulimit没有运气。不知道发生了什么。在 Ubuntu 16.04 上运行。

Tho*_*mas 4

由于 Ubuntu 16.04 使用systemd,您必须ulimit根据每个服务调整设置。为此,请创建一个文件/etc/systemd/system/mongodb.service.d/override.conf并覆盖默认值。

root@xenial:~# sudo systemctl edit mongodb.service
Run Code Online (Sandbox Code Playgroud)

粘贴它们:

[Service]
LimitNOFILE=infinity
LimitNPROC=infinity
Run Code Online (Sandbox Code Playgroud)

Ctrl + O 然后 Ctrl + X 退出并创建文件 /etc/systemd/system/mongodb.service.d/override.conf

root@xenial:~# cat /etc/systemd/system/mongodb.service.d/override.conf
[Service]
LimitNOFILE=infinity
LimitNPROC=infinity
Run Code Online (Sandbox Code Playgroud)

要检查是否应用了这些设置,您可以使用systemctl show。首先,让我们看看活跃的值。

root@xenial:~# systemctl --no-pager show mongodb.service | egrep 'NOFILE|NPROC'
LimitNOFILE=1024
LimitNOFILESoft=1024
LimitNPROC=7839
LimitNPROCSoft=7839
Run Code Online (Sandbox Code Playgroud)

然后应用设置。

root@xenial:~# systemctl daemon-reload 
root@xenial:~# systemctl --no-pager show mongodb.service | egrep 'NOFILE|NPROC'
LimitNOFILE=18446744073709551615
LimitNOFILESoft=18446744073709551615
LimitNPROC=18446744073709551615
LimitNPROCSoft=18446744073709551615
Run Code Online (Sandbox Code Playgroud)