MongoDB的安全设置包括多个方面,以下是一些关键的步骤和建议:
mongod.conf
,在 security
部分添加或确认以下行:security:
authorization: enabled
mongo -u admin -p --authenticationDatabase admin
use admin
db.createUser({
user: "myUserAdmin",
pwd: "myStrongPassword",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" }
]
})
admin
数据库存储用户名和密码。use admin
db.createRole({
role: "readOnly",
privileges: [
{ resource: { db: "mydatabase", collection: "" }, actions: [ "find" ] }
],
roles: []
})
db.grantRolesToUser("myuser", [
{ role: "readOnly", db: "mydatabase" }
])
mongod.conf
配置文件中添加以下参数:net:
ssl:
mode: requireSSL
PEMKeyFile: /path/to/your/ssl.pem
CAFile: /path/to/your/ca.pem
mongod.conf
配置文件中添加 bindIp
参数:net:
bindIp: 192.168.1.0/24
mongod.conf
配置文件中添加以下参数:security:
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/audit.json
通过以上步骤,可以显著提高MongoDB的安全性,保护数据库中的数据不被未经授权的用户访问或修改。