在Ubuntu上配置MongoDB时,有几个关键的网络配置注意事项需要考虑,以确保系统的安全性和稳定性。以下是详细的注意事项和建议:
/etc/mongod.conf
,设置 bindIp
为 0.0.0.0
或特定的服务器IP地址,以允许网络访问。ufw
防火墙,允许通过27017端口的流量:sudo ufw allow 27017
/etc/resolv.conf
中的默认设置(127.0.0.53),可以将其修改为公共DNS服务器,如223.5.5.5。mongod.conf
中设置 security.authorization
为 enabled
,然后重启MongoDB服务:security:
authorization: enabled
mongo
shell创建管理员用户,并启用认证:mongo -u admin -p yourpassword --authenticationDatabase admin
use admin
db.createUser({
user: 'admin',
pwd: 'yourpassword',
roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ]
})
mongod.conf
中设置 bindIp
为特定的IP地址,以限制访问范围。mongod.conf
中配置:net:
ssl:
mode: requireSSL
sslPEMKeyFile: /etc/ssl/mongodb.pem
sslCAFile: /etc/ssl/ca.pem
mongod.conf
中添加以下内容以启用审计日志:systemLog:
destination: file
format: JSON
path: /var/log/mongodb/audit.json
sudo iptables -A INPUT -p tcp --dport 27017 -s 192.168.1.0/24 -j ACCEPT
sudo iptables-persistent save
mongod.conf
中配置网络接口绑定,可以指定多个IP地址:net:
port: 27017
bindIp: 192.168.1.100,127.0.0.1
通过以上配置,您可以在Ubuntu上为MongoDB设置一个坚固的安全防线,确保只有授权的用户和设备能够访问数据库。请根据实际需求调整配置,并定期审查和更新安全设置。