Linux MongoDB分配权限

mongo.conf文件中加入 auth=true,开启权限功能

编辑文件

> sudo gedit /etc/mongo.conf

创建用户

进入mongodb

> mongo

必须先切换到admin用户下,来创建新用户

> use admin
switch to db admin

新建root用户
root用户拥有所有权限

> db.createUser({user:'Danile',pwd:'123456',roles:['root']})
Successfully added user: { "user" : "Danile", "roles" : [ "root" ] }

新建只能读test数据库的用户

> db.createUser({user:'temp',pwd:'123456',roles:[{role:'read',db:'test'}]})
Successfully added user: {
    "user" : "temp",
    "roles" : [
        {
            "role" : "read",
            "db" : "test"
        }
    ]
}

从客户端结束MongoDB进程

 db.shutdownServer()

使用用户登陆MongoDB

使用认证模式开启MongoDB服务

> sudo mongod --auth

进入服务后
除了登陆用户其他操作都是不被允许的

> show dbs
2018-08-02T22:16:59.658+0800 listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13
} at src/mongo/shell/mongo.js:47

登陆刚才注册的用户

> use admin
switched to db admin
> db.auth('Danile','123456')
1

该用户有root权限,可以干任何事

> show dbs
admin   0.078GB
local   0.078GB
person  0.078GB

切换到temp用户,temp用户只被允许能够操作test数据库,其他事情都做不了。

> db.auth('temp','123456')
1
> show dbs
2018-08-02T22:21:14.787+0800 listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13
} at src/mongo/shell/mongo.js:47

你可能感兴趣的:(Linux MongoDB分配权限)