在Linux系统下配置MongoDB以提高安全性,可以按照以下步骤进行:
/etc/mongod.conf
),在 security
部分添加或确认以下内容:security:
authorization: enabled
sudo systemctl restart mongod
mongo
命令连接到MongoDB实例。admin
数据库中创建一个管理员用户并为其分配必要的角色。例如:use admin
db.createUser({
user: "admin",
pwd: "yourStrongPassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
mongod.conf
文件中配置绑定的IP地址,只允许特定的网络访问MongoDB。例如,只允许本地访问:net:
bindIp: 127.0.0.1
mongod.conf
文件中添加SSL配置:net:
ssl:
mode: requireSSL
PEMKeyFile: /path/to/mongodb.pem
CAFile: /path/to/ca.pem
sudo systemctl restart mongod
mongod.conf
文件中禁用HTTP接口和JSONP访问,以减少攻击面:net:
http:
enabled: false
RESTInterfaceEnabled: false
JSONPEnabled: false
mongod.conf
文件中添加以下内容:security:
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/audit.json
sudo systemctl restart mongod
sudo chmod -R 750 /var/lib/mongodb
sudo chmod -R 750 /var/log/mongodb
iptables
或 ufw
限制对MongoDB端口的访问,确保只有受信任的网络可以访问MongoDB实例。例如,使用 ufw
:sudo ufw allow 27017/tcp
通过以上步骤,可以显著提高MongoDB在Linux上的安全性。