/etc/mongod.conf
,添加 security.authorization: enabled
,重启服务生效。admin
数据库,使用 db.createUser()
创建具有 root
角色的管理员账号。use admin
db.createUser({
user: "admin",
pwd: "强密码",
roles: [{ role: "root", db: "admin" }]
})
readWrite
(读写)、dbAdmin
(管理集合/索引)等角色。myuser
分配 mydatabase
数据库的读写权限use mydatabase
db.createUser({
user: "myuser",
pwd: "密码",
roles: [{ role: "readWrite", db: "mydatabase" }]
})
db.getUser("用户名")
。db.changeUserPassword("用户名", "新密码")
。db.dropUser("用户名")
。db.grantRolesToUser("用户名", ["角色1", "角色2"])
。db.revokeRolesFromUser("用户名", ["角色1", "角色2"])
。root
:超级权限,可管理所有数据库。readWriteAnyDatabase
:可读写所有非 local
/config
数据库。dbOwner
:等价于 readWrite
+dbAdmin
+userAdmin
。--authenticationDatabase
指定认证库(如 admin
)。