mongoDB开启auth验证后,除了创建用户和授权角色外,在执行一些特殊操作的时候,还是会“not authorized”错误,如在执行以下语句时
db.eval('db.whiteBoardUserEdit.find({})')
会报如下错误:
解决步骤如下:
1.新建一个角色sysAdmin
首先切换到admin库
db.createRole({
role: "sysAdmin",
privileges: [{
resource: {
anyResource: true
},
actions: ['anyAction']
}],
roles: []
})
2.将这个角色授权给当前用户(我的当前用户为admin)
首先还是要切换到admin库
db.grantRolesToUser(
"admin",
['sysAdmin',{ role: "sysAdmin", db: "admin" } ]
)
3.现在就可以正常执行以下语句了
注意: db.eval()功能强烈不建议使用,因为在4.2已经被官方移除了
IMPORTANT
Starting in version 4.2, MongoDB removes the eval command. The deprecated db.eval(), which wraps the eval command, can only be run against MongoDB 4.0 or earlier versions. For behavior and example, refer to the 4.0 or earlier version of the manual.
参考地址:
https://docs.mongodb.com/manual/reference/method/db.eval/#definition
https://docs.mongodb.com/manual/release-notes/4.2-compatibility/#remove-support-for-the-eval-command