MongoDB开启的 -auth 之后,无法使用 db.eval() 有关问题
MongoDB开启的 --auth 之后,无法使用 db.eval() 问题
MongoDB开启auth验证后,除了创建用户和授权角色外,在执行一些特殊操作的时候,还是会“not authorized”错误,如在执行eval时会报:
Command failed with error 13: 'not authorized on *** to execute command { $eval: "function()...
解决方案:
在官网 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。
2)新建一个角色,比如叫 sysadmin,需要先切换到admin库进行如下操作:
db.createRole({ role: "sysAdmin", privileges: [{ resource: { anyResource: true }, actions: ['anyAction'] }], roles: [] }
3)新建一个用户,比如叫 sysUser,授权sysAdmin角色
db.createUser({"user":"sysUser","pwd":"123456",roles:[]});
db.grantRolesToUser("sysUser",[{role:"sysAdmin",db:"admin"}]);
特别注意,grant时db一定是指明"admin"
4)开启--auth,试一下吧