在CentOS上設置MongoDB的安全措施主要包括以下幾個方面:
/etc/mongod.conf,在 security 部分啟用授權:security:
authorization: enabled
mongo shell連接到MongoDB并創建管理員用戶:use admin
db.createUser({
user: "admin",
pwd: "your_strong_password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
db.grantRolesToUser( "admin" , [ { role: "userAdminAnyDatabase" , db: "admin" } ])
/etc/mongod.conf 文件,添加SSL配置:net:
ssl:
mode: requireSSL
PEMKeyFile: /path/to/your/ssl.pem
CAFile: /path/to/your/ca.pem
sudo systemctl restart mongod
bind_ip 來限制數據庫服務器只在與該配置項關聯的接口上偵聽。例如,只允許本地訪問可以通過將 bindIp 設置為 127.0.0.1 。firewalld 配置防火墻規則,只允許特定IP地址訪問MongoDB端口(默認是27017):sudo firewall-cmd --permanent --zone=public --add-port=27017/tcp
sudo firewall-cmd --reload
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
通過以上步驟,可以顯著提高MongoDB在CentOS上的安全性。請根據實際需求和環境調整配置。