<span>mongodb角色管理和用户管理</span>

 

 

 

方法名 描述
db.getUsers()  
db.dropAllUsers()  
db.updateUser()  
db.createUser()  
db.revokeRolesFromUser()  
db.removeUser() 删除用户
db.grantRolesToUser()  
db.getUser()  
db.changeUserPassword()  
db.auth() 数据库认证、安全模式

 

 

 创建超级账号:要在admin数据库下面创建超级账号:

选择数据库:use admin

创建用户:db.createUser({user:"glc",pwd:"glc",roles:[{role:"root",db:"admin"}]})
检测密码:db.auth("username","password")



glc-test:PRIMARY> use admin
switched to db admin
glc-test:PRIMARY> db.createUser({user:"glc",pwd:"glc",roles:[{role:"root",db:"admin"}]})
Successfully added user: {
        "user" : "glc",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
glc-test:PRIMARY> db.auth("glc","glc")
1

 

 

 

 

 

 

创建某个库有创建索引的权限:

replset_name:PRIMARY> use admin
switched to db admin
replset_name:PRIMARY> db.createRole({role:"role_name",privileges: [{ resource: { db: "db_name", collection: "" }, actions: [ "createIndex" ]}],roles:[]})

 

在 MongoDB 中创建了很多帐号,怎么查看所有帐号信息?

1. 查看全局所有账户 

2. 查看当前库下的账户 

 

查看全局所有账户 :

>  use admin
switched to db admin
> db.auth('admin','123456')
1
> db.system.users.find().pretty()
{
    "_id" : "admin.root",
    "user" : "root",
    "db" : "admin",
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : 10000,
            "salt" : "******************==",
            "storedKey" : "*****************=",
            "serverKey" : "**************="
        }
    },
    "roles" : [
        {
            "role" : "read",
            "db" : "admin"
        },
        {
            "role" : "readWrite",
            "db" : "admin"
        },
        {
            "role" : "dbAdmin",
            "db" : "admin"
        },
        {
            "role" : "dbAdmin",
            "db" : "devicedb"
        }
    ]
}
{
    "_id" : "test.test",
    "user" : "test",
    "db" : "test",
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : 10000,
            "salt" : "******************==",
            "storedKey" : "******************=",
            "serverKey" : "******************="
        }
    },
    "roles" : [
        {
            "role" : "read",
            "db" : "test"
        },
        {
            "role" : "readWrite",
            "db" : "test"
        },
        {
            "role" : "dbAdmin",
            "db" : "test"
        },
        {
            "role" : "userAdmin",
            "db" : "test"
        },
        {
            "role" : "dbOwner",
            "db" : "test"
        }
    ]
}

 

查看当前库下的账户 :

> use admin
switched to db admin
> show users
{
    "_id" : "admin.root",
    "user" : "root",
    "db" : "admin",
    "roles" : [
        {
            "role" : "read",
            "db" : "admin"
        },
        {
            "role" : "readWrite",
            "db" : "admin"
        },
        {
            "role" : "dbAdmin",
            "db" : "admin"
        },
        {
            "role" : "userAdmin",
            "db" : "admin"
        },
        {
            "role" : "dbAdmin",
            "db" : "devicedb"
        }
    ]
}
> use test
switched to db test
> show users
{
    "_id" : "test.test",
    "user" : "test",
    "db" : "test",
    "roles" : [
        {
            "role" : "read",
            "db" : "test"
        },
        {
            "role" : "readWrite",
            "db" : "test"
        },
        {
            "role" : "dbAdmin",
            "db" : "test"
        },
        {
            "role" : "userAdmin",
            "db" : "test"
        },
        {
            "role" : "dbOwner",
            "db" : "test"
        }
    ]
}

 

 

 

 

####################################

全部评论

相关推荐

11-18 15:57
门头沟学院 Java
最终归宿是测开:这个重邮的大佬在重邮很有名的,他就喜欢打92的脸,越有人质疑他,他越觉得爽😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务