在 Linux 上加速 MongoDB,通常从硬件、系统、MongoDB 配置、索引、查询几个层面入手。下面按优先级给你一份实用清单。
这是最有效的提升手段。
# /etc/fstab
/dev/sdb1 /data ext4 defaults,noatime 0 0
或临时:
mount -o remount,noatime /data
# 提高文件描述符
echo "* soft nofile 64000" >> /etc/security/limits.conf
echo "* hard nofile 64000" >> /etc/security/limits.conf
# 内核参数
sysctl -w vm.swappiness=1
sysctl -w vm.dirty_ratio=10
sysctl -w vm.dirty_background_ratio=5
yum install tuned -y
tuned-adm profile throughput-performance
storage:
dbPath: /data/mongodb
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 8 # 建议 = 内存的 50%~70%
collectionConfig:
blockCompressor: snappy
storage:
journal:
enabled: false # ⚠ 有数据丢失风险
net:
port: 27017
bindIp: 0.0.0.0
maxIncomingConnections: 65536
db.orders.createIndex({ user_id: 1, created_at: -1 })
db.setProfilingLevel(1, { slowms: 100 })
db.system.profile.find().sort({ ts: -1 }).limit(10)
db.orders.find({ status: "paid" }).explain("executionStats")
确保 IXSCAN 而不是 COLLSCAN
db.users.find({}, { name: 1, age: 1 })
db.collection.insertMany([...])
readPreference=secondaryecho never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
如果你愿意,可以告诉我:
我可以给你更精准的配置模板。