MongoDB开启的 --auth 之后,无法使用 db.eval() 问题

本文介绍了解决MongoDB开启auth认证后执行eval命令时出现的权限问题。通过创建具有anyAction权限的角色并分配给特定用户,使得该用户能够执行eval等敏感操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     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,试一下吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值