以下是配置Ubuntu MongoDB安全的关键步骤:
安装MongoDB
使用官方仓库安装最新版:
sudo apt update
sudo apt install -y mongodb-org
启用认证
编辑配置文件 /etc/mongod.conf
,添加:
security:
authorization: enabled
重启服务生效:
sudo systemctl restart mongod
创建管理员用户
进入MongoDB Shell,切换到admin
数据库并创建用户:
use admin
db.createUser({
user: "admin",
pwd: "强密码",
roles: [{ role: "root", db: "admin" }]
})
之后需使用该用户认证登录:
mongo -u admin -p "强密码" --authenticationDatabase admin
限制IP访问
在配置文件中设置bindIp
为特定IP(如127.0.0.1
或服务器IP),避免暴露在公网:
net:
bindIp: 127.0.0.1
重启服务:
sudo systemctl restart mongod
配置防火墙
使用ufw
限制MongoDB端口(默认27017)的访问:
sudo ufw allow from <信任IP> to any port 27017
sudo ufw enable
启用加密(可选)
net:
ssl:
mode: requireSSL
sslPEMKeyFile: /path/to/mongodb.pem
sslCAFile: /path/to/ca.pem
定期维护
mongodump
定期备份数据库。systemLog
路径及格式)。更新与补丁管理
定期检查MongoDB官方更新,及时安装安全补丁。
注意:生产环境中需根据实际需求调整权限和加密策略,避免使用默认配置。参考来源:[1,2,3,4,5,6,7,8,9,10,11]