在Linux下对MongoDB进行安全配置,可以遵循以下步骤:
/etc/mongod.conf
文件中,找到或添加以下内容:security:
authorization: enabled
sudo systemctl restart mongod
mongo
use admin
db.createUser({
user: "admin",
pwd: "your_strong_password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
/etc/mongod.conf
文件中配置 bindIp
参数,仅允许特定IP地址或主机名连接。例如,只允许本机访问:net:
bindIp: 127.0.0.1
/etc/mongod.conf
文件中添加:net:
http:
enabled: false
sudo openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout /etc/ssl/mongodb.key -out /etc/ssl/mongodb.crt
/etc/mongod.conf
文件,添加SSL配置:net:
ssl:
mode: requireSSL
PEMKeyFile: /etc/ssl/mongodb.key
CAFile: /etc/ssl/mongodb.crt
sudo systemctl restart mongod
/etc/mongod.conf
文件中添加以下参数以启用审计日志记录所有用户的操作:security:
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/audit.json
sudo systemctl restart mongod
sudo iptables -A INPUT -p tcp --dport 27017 -s 127.0.0.1 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 27017 -j DROP
sudo ufw allow from 127.0.0.1 to any port 27017
sudo ufw disable
mongodump
和 mongorestore
工具进行备份和恢复。