以下是Linux上MongoDB的安全设置步骤:
启用认证
编辑配置文件/etc/mongod.conf,添加或修改:
security:
authorization: enabled # 启用用户认证
重启服务:sudo systemctl restart mongod。
创建管理员账户
连接MongoDB Shell,切换到admin数据库并创建管理员用户:
use admin
db.createUser({
user: "admin",
pwd: "强密码",
roles: ["root"] // 超级管理员角色
})
之后需使用认证信息连接数据库。
限制网络访问
bindIp为特定IP(如127.0.0.1仅允许本地访问):net:
bindIp: 127.0.0.1
ufw或iptables)限制端口(默认27017)访问。禁用不必要的接口
在配置文件中关闭HTTP和REST接口:
net:
http:
enabled: false
RESTInterfaceEnabled: false
```。
使用TLS/SSL加密
net:
ssl:
mode: requireSSL
PEMKeyFile: /path/to/ssl.pem
CAFile: /path/to/ca.pem
配置审计日志(企业版)
在配置文件中添加:
security:
auditLog:
destination: file
path: /var/log/mongodb/audit.log
```。
权限管理与最小权限原则
readWrite),避免使用root权限。其他安全措施
mongodump --out /backup/path。root。参考来源:[1,2,3,4,5,6,7,8,9,10,11]