mongodb用户创建以及权限控制

2018-08-06  本文已影响0人  yandaren

我们在安装完mongodb之后,默认是没有开启权限认证的, 但是我们在生产环境权限认证是必不可少的

1. 创建用户

C:\Users\xxx>mongo
> use admin
switched to db admin
> db.createUser(
... {
...   user: "admin",
...   pwd: "123456",
...   roles: [ { role: "userAdminAnyDatabase", db: "admin"} ]
... }
... )
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
>

2. 开启权限认证

如果是命令模式启动的话,就在原来的启动参数上,在加上 --auth 即可

c:\> mongod --auth --dbpath "D:\Program Files\MongoDB\Server\4.0\data"

如果是windows service方式运行的话,打开 mongo配置文件mongod.cfg, 在security项下,将authorization设置为enabled, 默认是disabled

security:
  authorization: enabled

然后重启service就可以了

3. 以认证的方式连接mongo

C:\Users\xxx>mongo -port 27017 -u "admin"  -p "123456" --authenticationDatabas
e "admin"
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 4.0.1
> use admin
switched to db admin
> show users;
{
        "_id" : "admin.admin",
        "user" : "admin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}
>
C:\Users\xxx>mongo
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1
> use admin
switched to db admin
> db.auth("admin", "123456")
1
> show users;
{
        "_id" : "admin.admin",
        "user" : "admin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}
>

4. 根据需要创建其他的账号

> use admin
switched to db admin
> db.createUser(
... {
...  user : "my_tester",
...  pwd : "123456",
...  roles: [ { role : "readWrite", db : "test" } ,
...           { role : "read", db : "game_report" } ]
... }
... )
Successfully added user: {
        "user" : "my_tester",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "test"
                },
                {
                        "role" : "read",
                        "db" : "game_report"
                }
        ]
}
C:\Users\xxx>mongo
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1
> use admin
switched to db admin
> db.auth("my_tester", "123456")
1
> use test
switched to db test
> db.foo.insert( { x : 1, y : 2 } )
WriteResult({ "nInserted" : 1 })
> db.foo.find()
{ "_id" : ObjectId("5b67fc008bd89ebd4abc54aa"), "x" : 1, "y" : 2 }
>

5. 权限说明(基于角色的权限控制)

5.1 内置角色

数据库用户角色
数据库管理角色
备份和还原角色
夸库角色
集群管理
超级权限

5.2 自定义角色

内置角色只能控制User在DB级别上执行的操作,管理员可以创建自定义角色,控制用户在集合级别(Collection-Level)上执行的操作,即,控制User在当前DB的特定集合上执行特定的操作

6. 参考连接

上一篇下一篇

猜你喜欢

热点阅读