在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上的安全性。請記住,安全是一個持續的過程,需要定期審查和更新配置。