环境:AlmaLinux 10.1 / 4C8G调优 / MongoDB 8.0.23 目标:2000并发生产数据库
安装包:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel93-8.0.23.tgz
RHEL9 编译,兼容 AlmaLinux 10
cd /root
tar -xzf mongodb-linux-x86_64-rhel93-8.0.23.tgz
mv mongodb-linux-x86_64-rhel93-8.0.23 /usr/local/mongo
groupadd -g 27017 mongo
useradd -u 27017 -g mongo -s /sbin/nologin -M mongo
mkdir -p /usr/local/mongo/{conf,data,log,run}
chown -R mongo:mongo /usr/local/mongo
cat > /usr/local/mongo/conf/mongod.conf << 'EOF'
# MongoDB 8.0 生产配置 - 按4C8G调优,目标2000并发
storage:
dbPath: /usr/local/mongo/data
wiredTiger:
engineConfig:
cacheSizeGB: 6
journalCompressor: snappy
collectionConfig:
blockCompressor: snappy
indexConfig:
prefixCompression: true
systemLog:
destination: file
logAppend: true
path: /usr/local/mongo/log/mongod.log
logRotate: rename
net:
bindIp: 127.0.0.1,172.30.30.6
port: 27017
maxIncomingConnections: 4000
wireObjectCheck: true
processManagement:
fork: true
pidFilePath: /usr/local/mongo/run/mongod.pid
security:
authorization: enabled
operationProfiling:
mode: slowOp
slowOpThresholdMs: 100
EOF
chown mongo:mongo /usr/local/mongo/conf/mongod.conf
cat > /etc/sysctl.d/99-mongodb.conf << 'EOF'
vm.swappiness=10
vm.dirty_ratio=15
vm.dirty_background_ratio=5
net.core.somaxconn=65535
net.ipv4.tcp_max_syn_backlog=65535
fs.file-max=100000
EOF
sysctl -p /etc/sysctl.d/99-mongodb.conf
cat > /etc/security/limits.d/99-mongodb.conf << 'EOF'
mongo soft nofile 65535
mongo hard nofile 65535
mongo soft nproc 65535
mongo hard nproc 65535
EOF
MongoDB 8.0 使用 tcmalloc-google 分配器,需按以下配置:
# 临时生效
echo always > /sys/kernel/mm/transparent_hugepage/enabled
echo defer+madvise > /sys/kernel/mm/transparent_hugepage/defrag
echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none
# 开机自动配置
cat > /etc/systemd/system/disable-thp.service << 'EOF'
[Unit]
Description=Configure THP for MongoDB tcmalloc-google
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo always > /sys/kernel/mm/transparent_hugepage/enabled && echo defer+madvise > /sys/kernel/mm/transparent_hugepage/defrag && echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none'
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable disable-thp
cat > /etc/systemd/system/mongod.service << 'EOF'
[Unit]
Description=MongoDB 8.0 Database Server
After=network.target
[Service]
Type=forking
User=mongo
Group=mongo
Environment=GLIBC_TUNABLES=glibc.pthread.rseq=0
ExecStart=/usr/local/mongo/bin/mongod --config /usr/local/mongo/conf/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/mongo/bin/mongod --shutdown --config /usr/local/mongo/conf/mongod.conf
Restart=always
RestartSec=5
LimitNOFILE=65535
LimitNPROC=65535
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable mongod
curl -sL https://downloads.mongodb.com/compass/mongosh-2.8.3-linux-x64.tgz -o /root/mongosh.tgz
tar -xzf /root/mongosh.tgz -C /usr/local/mongo/ --strip-components=1 --wildcards '*/bin/*'
systemctl start mongod
mongosh --quiet --eval 'db.getSiblingCode("admin").createUser({user: "root", pwd: "123456", roles: [{role: "root", db: "admin"}]})'
mongosh -u root -p 123456 --authenticationDatabase admin --eval 'db.getSiblingDB("admin").runCommand({ping:1})'
# 期望输出: { ok: 1 }
/usr/local/mongo/
├── bin/ # 二进制程序(mongod, mongos, mongosh)
├── conf/ # 配置文件
│ └── mongod.conf
├── data/ # 数据文件
├── log/ # 运行日志
│ └── mongod.log
└── run/ # PID文件
└── mongod.pid
| 参数 | 值 | 说明 |
|---|---|---|
| wiredTiger.cacheSizeGB | 6 | 8G内存的75%,留2G给系统 |
| maxIncomingConnections | 4000 | 2000并发 × 2倍冗余 |
| blockCompressor | snappy | 平衡压缩率和CPU |
| prefixCompression | true | 索引前缀压缩,节省内存 |
| slowOpThresholdMs | 100 | 慢查询阈值 |
| journalCompressor | snappy | Journal压缩 |
| vm.swappiness | 10 | 尽量不用swap |
| nofile | 65535 | 满足高并发连接需求 |
# 启动 / 停止 / 重启
systemctl start mongod
systemctl stop mongod
systemctl restart mongod
# 查看状态
systemctl status mongod
# 连接数据库
mongosh -u root -p 123456 --authenticationDatabase admin
# 查看启动告警(应为空)
mongosh -u root -p 123456 --authenticationDatabase admin --eval 'db.adminCommand({getLog:"startupWarnings"})'
# 查看连接数
mongosh -u root -p 123456 --authenticationDatabase admin --eval 'db.serverStatus().connections'
# 查看缓存使用
mongosh -u root -p 123456 --authenticationDatabase admin --eval 'db.serverStatus().wiredTiger.cache'
# 查看日志
tail -f /usr/local/mongo/log/mongod.log