[MongoDB] not authorized on test to execute command-MongoDB的权限配置

直接报Command ‘$eval’ failed: not authorized on这个错误,可以确认是权限的问题

解决方案

在官网 http://docs.mongodb.org/manual/reference/command/eval/#dbcmd.eval 有一段描述:

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

解决步骤

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

/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --fork

或者在配置文件中修改:

auth = false

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

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

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

> db.createUser({
    user:'your_user_name',
    pwd:'your_password',
    roles:[
        {role:'sysadmin',db:'admin'}
    ]
})

好了现在重启启动数据库带上–auth。

/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --fork --auth

或者在配置文件中修改:

auth = true

启动成功之后,使用上面创建的用户进行连接(连接时指定用户名和密码)。之后就可以正常执行了

[mondba@hostmon scripts]$ mongo admin --port your_port -u your_user_name -p your_password
MongoDB shell version: 2.6.0
connecting to: 127.0.0.1:your_port/admin

本文转自: [MongoDB] not authorized on test to execute command-MongoDB的权限配置

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值