mongodb在启用鉴权的数据库上执行copyto 报错 not authorized on library to execute command

时间:2022-12-04 09:55:05

1.db.media.copyTo('test1')报错

mongodb在启用鉴权的数据库上执行copyto 报错 not authorized on library to execute command

解决办法:

在官网 ​​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.

2.解决步骤

新建一个角色

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

mongodb在启用鉴权的数据库上执行copyto 报错 not authorized on library to execute command

在现有的用admin上增加 sysadmin 角色

use admin
db.grantRolesToUser( "admin", [ { role: "sysadmin",db:"admin"} ] )

mongodb在启用鉴权的数据库上执行copyto 报错 not authorized on library to execute command

3.切换到目标数据库执行db.media.copyTo('test1')

mongodb在启用鉴权的数据库上执行copyto 报错 not authorized on library to execute command