CentOS下MongoDB内存分配与调优
一 内存分配机制与默认值
二 在CentOS上配置WiredTiger缓存
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 4
db.adminCommand({
setParameter: 1,
wiredTigerEngineRuntimeConfig: "cache_size=4294967296" // 单位字节,示例为4GB
})
sudo systemctl restart mongodmongo --eval 'db.serverStatus().mem'db.serverStatus().wiredTiger.cache三 操作系统与内核参数建议
sudo sysctl vm.swappiness=1(或在/etc/sysctl.conf中持久化)sudo sysctl vm.overcommit_memory=2echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled/etc/rc.local或systemd服务中执行相同命令,或按发行版文档配置* soft nofile 65536、* hard nofile 65536net.core.somaxconn=65535、net.ipv4.ip_local_port_range=1024 65535sudo sysctl -p 并重启MongoDB服务。四 监控与容量规划
mongostat --all:观察mem.resident(常驻内存)、page_faults、队列与连接等指标mongotop:按集合查看读写耗时db.serverStatus().mem:查看内存使用概览db.serverStatus().wiredTiger.cache:查看WiredTiger缓存命中率、脏页比例、最大/已用缓存等五 常见场景与注意事项