在 Linux 上修改 MongoDB 配置,通常涉及配置文件修改和服务重启/重载。下面按常见场景说明。
常见路径(取决于安装方式):
/etc/mongod.conf/opt/mongodb/mongod.conf查看是否正在运行及配置文件:
ps -ef | grep mongod
MongoDB 3.0+ 默认使用 YAML 配置。
sudo vi /etc/mongod.conf
net:
bindIp: 0.0.0.0
port: 27017
⚠️ 生产环境不建议直接
0.0.0.0,应限制 IP 并开启鉴权
security:
authorization: enabled
storage:
dbPath: /var/lib/mongo
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
# 重启
sudo systemctl restart mongod
# 或重载(配置支持 reload 时)
sudo systemctl reload mongod
# 查看状态
sudo systemctl status mongod
sudo service mongod restart
mongod --config /path/to/mongod.conf
mongo
> db.runCommand({ getCmdLineOpts: 1 })
或查看日志:
tail -f /var/log/mongodb/mongod.log
net:
port: 27018
mongod.conf 中开启 authorization: enabledsystemLog:
...
net:
maxIncomingConnections: 65536
bindIp 后需确保防火墙放行dbPath 需先停止服务并迁移数据如果你能告诉我:
我可以给你精确修改示例。