在CentOS上配置MongoDB的安全措施主要包括以下几个方面:
启用账户认证:
/etc/mongod.conf,设置 security.authorization 为 enabled。security: authorization: enabled sudo systemctl restart mongod 创建和管理用户:
readonlyUser 的用户,该用户具有只读权限:use yourDatabase; db.createUser({ user: "readonlyUser", pwd: "readonlyPass", roles: [ { role: "read", db: "yourDatabase" } ] }); 修改默认端口:
/etc/mongod.conf 文件,修改 net.port 为所需的端口号(例如27017)。net: port: 27017 配置网络访问控制:
bindIp 选项限制MongoDB服务器只监听特定IP地址。例如,只允许本机访问:net: bindIp: 127.0.0.1 启用TLS/SSL:
mongod.conf 中添加以下配置:net: ssl: mode: requireSSL PEMKeyFile: /path/to/mongodb-cert.pem 定期更新和补丁管理:
使用防火墙限制访问:
iptables:sudo iptables -A INPUT -p tcp --dport 27017 -j ACCEPT sudo service iptables save 启用审计日志(可选):
mongod.conf 中启用审计日志:security: auditLog: destination: file format: JSON path: /var/log/mongodb/audit.json 限制远程访问:
bindIp 和防火墙实现。通过以上步骤,可以显著提高MongoDB在CentOS上的安全性。请根据你的具体需求和环境调整配置。