在Ubuntu上配置MongoDB内存时,主要通过调整WiredTiger存储引擎的缓存大小来优化性能,以下是具体建议:
storage.wiredTiger.engineConfig.cacheSizeGB
设置为服务器物理内存的50%-80%,例如4GB、8GB等。storage.journal.enabled: true
,提升数据持久性和恢复能力。storage.wiredTiger.engineConfig.journalCompressor: snappy
压缩日志,节省空间。db.serverStatus().wiredTiger.cache
命令查看缓存使用情况,根据实际负载调整。配置示例(/etc/mongod.conf):
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 4 # 根据服务器内存调整
journalCompressor: snappy
journal:
enabled: true
注意:避免将缓存设置过高,以免占用过多内存导致系统或其他服务异常。