mongo createUser updateUser
2024-07-25mongo createUser dropUser updateUser dropUser grantRolesToUser revokeRolesFromUser
1、use admin;
show users
2、create root
db.createUser(
{
user: "root",
pwd: "newpassword",
roles: [ { role: "root", db: "admin" } ]
}
)
2、create user
use db1;
db.createUser(
{
user:"$user",
pwd:"$pw",
roles:
[
{role:"read",db:"db1"},
{role:"read",db:"db2"},
{role:"read",db:"db3"}
]
})
use crmdb
db.createCollection('test')
db.createUser(
{
user:"crmuser",
pwd:"password2",
roles:[{role:"dbOwner",db:"crmdb"}]
})
3、db.grantRolesToUser(
"root",
"db" : "admin",
[
{ role: "myNewRole", db: "myDatabase" }
]
);
4、如果你还想移除用户的某些角色,可以使用db.revokeRolesFromUser命令
db.revokeRolesFromUser(
"myUser",
[
{ role: "myOldRole", db: "myDatabase" }
]
);
5、修改用户密码
db.updateUser( "admin",{pwd:"password"});
db.changeUserPassword('crmuser','newpasswd');
修改role权限
use crmdb
db.updateUser("crmuser", {
roles: ["dbOwner"]
})
6、删除用户(需要root权限,会将所有数据库中的football用户删除)
db.system.users.remove({user:"football"});
删除用户(权限要求没有那么高,只删除本数据中的football用户)
db.dropUser("football");
7、use db1
db.dropDatabase()
8、db.auth("root","password");
9、role参考:https://www.mongodb.com/zh-cn/docs/manual/reference/built-in-roles/
分类:数据库 | 标签: |