在Ubuntu上为MongoDB进行安全配置是确保数据库安全和防止未授权访问的重要步骤。以下是一些关键的安全配置要点:
/etc/mongod.conf
,在 security
部分启用身份验证:security:
authorization: enabled
sudo systemctl restart mongod
mongo
use admin
db.createUser({
user: "myUserAdmin",
pwd: "myUserAdminPwd",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
bindIp
,以允许特定IP地址访问MongoDB服务器:net:
port: 27017
bindIp: 127.0.0.1,192.168.1.100
sudo systemctl restart mongod
ufw
命令限制对MongoDB端口的访问:sudo ufw allow from 192.168.1.100 to any port 27017
sudo ufw enable
mongod.conf
配置文件中添加以下参数:net:
ssl:
mode: requireSSL
PEMKeyFile: /path/to/your/ssl.pem
CAFile: /path/to/your/ca.pem
sudo systemctl restart mongod
mongod.conf
配置文件中添加以下参数以启用审计日志记录所有用户的操作:systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
/etc/systemd/system/disable-thp.service
文件:[Unit]
Description = Disable Transparent Huge Pages (THP)
[Service]
Type = simple
ExecStart = /bin/sh -c "echo 'never' /sys/kernel/mm/transparent_hugepage/enabled && echo 'never' /sys/kernel/mm/transparent_hugepage/defrag"
[Install]
WantedBy = multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now disable-thp.service
/etc/security/limits.d/mongodb.conf
文件:mongod soft nproc 64000
mongod hard nproc 64000
mongod soft nofile 64000
mongod hard nofile 64000
通过以上步骤,可以显著提高MongoDB在Ubuntu上的安全性。请记住,安全是一个持续的过程,需要定期审查和更新配置。