在 CentOS 上让 MongoDB 免密登录(无密码访问),常见有几种方式,按使用场景说明:
vi /etc/mongod.conf
找到 security 部分,确保 没有 以下内容:
security:
authorization: enabled
或者直接写成:
security:
authorization: disabled
systemctl restart mongod
mongo
# 或
mongosh
✅ 此时 无任何账号密码校验
⚠️ 风险:任何人都能访问,公网绝对不要用
MongoDB 默认允许 本机通过 localhost 免密管理
只要:
authorizationmongo
适用于:
use admin
db.createUser({
user: "admin",
pwd: "123456",
roles: ["root"]
})
security:
authorization: enabled
echo "123456" > /etc/mongo_pass
chmod 600 /etc/mongo_pass
连接:
mongo -u admin -p $(cat /etc/mongo_pass) --authenticationDatabase admin
或 MongoDB 6+:
mongosh -u admin -p $(cat /etc/mongo_pass) --authenticationDatabase admin
适合 副本集 / 分片
security:
authorization: enabled
keyFile: /etc/mongo-keyfile
openssl rand -base64 756 > /etc/mongo-keyfile
chmod 600 /etc/mongo-keyfile
| 场景 | 推荐方式 |
|---|---|
| 本地测试 | 关闭 authorization |
| 生产单机 | 本地 localhost 管理 |
| 脚本自动 | 密码文件 |
| 集群 | keyFile |
如果你能说明:
我可以给你 最安全的具体配置示例。