在CentOS中配置MongoDB内存主要通过修改配置文件和使用系统工具实现,具体方法如下:
修改配置文件(推荐)
编辑 /etc/mongod.conf
,找到 storage
部分,通过 wiredTiger.engineConfig.cacheSizeGB
参数设置缓存大小(单位:GB),例如:
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 4 # 设置为4GB,可根据服务器内存调整
保存后重启服务:sudo systemctl restart mongod
。
命令行参数配置
启动时通过 --wiredTigerCacheSizeGB
参数指定缓存大小,如:
mongod --wiredTigerCacheSizeGB=2
(需在启动脚本或服务文件中添加)。
系统级限制(可选)
/etc/systemd/system/mongod.service
的 [Service]
中添加 MemoryLimit
参数,如 MemoryLimit=4G
,然后执行 sudo systemctl daemon-reload && sudo systemctl restart mongod
。验证配置
使用命令查看内存使用情况:
mongo --eval 'db.serverStatus().wiredTiger.cache'
,确认缓存大小是否符合预期。
注意事项: