设为首页 加入收藏

TOP

MongoDB的权限配置:开启auth之后的eva l权限
2015-11-21 02:00:50 来源: 作者: 【 】 浏览:0
Tags:MongoDB 权限 配置 开启 auth 之后 eva

本文为工作日志,解决当打开MongoDB的 --auth 之后,导致无法使用 db.eva l() 的问题。

问题描述:

使用--auth启动MongoDB,登录成功后,执行db.eva l,报如下错误:

?

> db.eva l('return 1111')
2015-03-04T15:18:54.062+0800 {
	"ok" : 0,
	"errmsg" : "not authorized on test to execute command { $eva l: \"return 1111\" }",
	"code" : 13
} at src/mongo/shell/db.js:403
>

?

解决方案:

If authorization is enabled, you must have access to all actions on all resources in order to run eva l. Providing such access is not recommended, but if your organization requires a user to run eva l, create a role that grants anyAction on anyResource. Do not assign this role to any other user.

解决步骤:

1)不带--auth参数启动数据库,所以不需要帐号即可连上MongoDB。

2)新建一个角色,比如叫 sysadmin,需要先切换到admin库进行如下操作:

?

> use admin
switched to db admin
> db.createRole({role:'sysadmin',roles:[],
... privileges:[
... {resource:{anyResource:true},actions:['anyAction']}
... ]})

3)然后,新建一个用户,使用这个角色,注意,这个角色的db是admin,操作如下:

?

?

> use woplus
switched to db woplus
> db.createUser({
... user:'woplus',
... pwd:'wo@1990',
... roles:[
... {role:'sysadmin',db:'admin'}
... ]})

4)OK,我们看看效果。先看看用户情况,如下:

?

?

> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "dfda4d4e75995650c3e1b3f2b65b1920" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "woplus.woplus", "user" : "woplus", "db" : "woplus", "credentials" : { "MONGODB-CR" : "bcabae4fe4d7951fad37cb2d099437e8" }, "roles" : [ { "role" : "sysadmin", "db" : "admin" } ] }

然后,我们重启数据库,加上 --auth 参数。

?

再从客户端使用帐号登录MongoDB。

?

./mongo -u woplus -p wo@1990  192.168.1.84/woplus
MongoDB shell version: 2.6.8
connecting to: 192.168.1.84/woplus
> 
> use woplus
switched to db woplus
> db.eva l('return 222')
222
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MongoDB数据库备份与恢复 下一篇解决方案:在cmd中无法向数据库中..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: