mongod 对指定数据库创建用户

https://blog.51cto.com/wzlinux/2153062?source=dra

1、先在admin库中创建管理员用户与密码

[root@mbasic ~]# mongo
MongoDB shell version: 3.2.6
connecting to: test
> use admin    //切换到admin数据库
switched to db admin
> db.createUser(   //创建用户
...   {
...     user: "myUserAdmin",
...     pwd: "abc123",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]   //指定数据库权限
...   }
... )
Successfully added user: {
        "user" : "myUserAdmin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
> 

 

2、先用管理员帐号密码登录admin库,然后切换到要设置的库(test)然后在使用db.createUser命令创建用户即可

[root@mbasic ~]# mongo
MongoDB shell version: 3.2.6
connecting to: test
> use admin
switched to db admin
> db.auth('myUserAdmin','abc123')
1
> use test
switched to db test
> db.createUser(
...    {
...      user:"test1",
...      pwd: "test1",
...      roles: [{ role: "readWrite", db: "test"}]    //对test库赋予readWrite权限
...    }
...  )
Successfully added user: {
        "user" : "test1",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "test"
                }
        ]
}
>
posted @ 2019-11-04 13:55  西瓜大  阅读(776)  评论(0编辑  收藏  举报