Mongodb

即使在設置限制後,MongoDB“打開的文件太多”

  • March 5, 2020

當 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

我的limits.conf包含這個

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

我也試過使用ulimit沒有運氣。不知道發生了什麼。在 Ubuntu 16.04 上執行。

由於 Ubuntu 16.04 使用systemd,您必鬚根據ulimit每個服務調整設置。為此,請創建一個文件/etc/systemd/system/mongodb.service.d/override.conf並覆蓋預設值。

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

粘貼它們:

[Service]
LimitNOFILE=infinity
LimitNPROC=infinity

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

要檢查是否應用了這些設置,您可以使用systemctl show. 首先,讓我們看看活躍的值。

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

然後應用設置。

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

引用自:https://serverfault.com/questions/837315