MongoDB - 无权执行命令

标签 mongodb shell authentication roles createuser

我已成功在 MongoDB 上启用授权,并在 admin 数据库上创建了一个帐户,然后为我的数据库创建了一个名为 test 的帐户。以下连接字符串可以成功连接到我的测试数据库:mongo --host 192.168.17.52 --port 27017 -u user1 -p password --authenticationDatabase test
我现在唯一的问题是,我无法执行诸如:show dbs 之类的命令。当我尝试这样做时,我收到以下错误:

"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, lsid: { id: UUID(\"a1d5bc0d-bc58-485e-b232-270758a89455\") }, $db: \"admin\" }"

我一直在许多在线资源上帮助解决这个问题,但没有运气,有没有办法解决这个问题?似乎我的用户无法访问 admin 数据库,有没有办法将此访问权限授予我的用户,以便我可以运行必要的命令,例如 show dbs?

任何帮助深表感谢! :)

最佳答案

为了运行show dbs命令,如果用户可以访问多个数据库,首先应该在 admin 上创建用户。数据库(这是因为 listDatabases 操作是集群范围的操作)。此外,应授予用户访问此操作的权限。为此,应使用该操作创建一个新角色。以下是相同的步骤:

//登录为admin--authenticationDatabase "admin" (假设 admin 用户具有 root 权限)然后运行以下命令:

use admin;

db.runCommand({ createRole: "listDatabases", privileges: [{ resource: { cluster : true }, actions: ["listDatabases"]} ], roles: [] });

db.createUser({user:"testUser", pwd:"passwd", roles:[{role:"read", db:"db1"},{role:"read", db:"db2"},{ role: "listDatabases", db: "admin" }]});

//以管理员用户身份退出并以testUser身份登录:注意--authenticationDatabase "admin"
mongo -u "testUser" -p --authenticationDatabase "admin"

登录后运行下面的命令,它应该列出所有数据库:
show dbs;

即使用户没有访问 admin 的权限,以下内容也能正常工作数据库:
use admin;

但是接下来会出现错误:
show collections;

关于MongoDB - 无权执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54026506/

相关文章:

linux - 我的 "ssh + nohup"脚本有什么问题

物理内存的Linux命令,只获取值

authentication - 无需注册应用程序即可通过 REST 进行 Office 365 身份验证

c# - PHP 可以进行表单例份验证票证解密吗?

android instagram 在不使用 webview 的情况下登录

node.js - Mongoose 可以需要子文档吗?

mongodb - pyspark-mongodb 集合读取命令不会执行

linux - 在 bash 中使用单引号仍然给出 "event not found"

mongodb - 如何在带有React Router的meteor mongo查询中使用参数

json - 如何在mongodb中导入json文件?