MongoDB 未授权查询 - 代码 13

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23619018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:42:39  来源:igfitidea点击:

MongoDB not authorized for query - code 13

mongodbmongodb-roles

提问by Guy

In MongoDB 2.6.1 I've setup a user with dbAdmin rights:

在 MongoDB 2.6.1 中,我设置了一个具有 dbAdmin 权限的用户:

{
        "_id" : "mydbname.myusername",
        "user" : "myusername",
        "db" : "mydbname",
        "credentials" : {
                "MONGODB-CR" : "<some credentials>"
        },
        "roles" : [
                {
                        "role" : "dbAdmin",
                        "db" : "mydbname"
                }
        ]
}

When I use the mongo shell to connect to the database (using -u and -p on command line) and run a query like this:

当我使用 mongo shell 连接到数据库(在命令行上使用 -u 和 -p)并运行如下查询时:

db.mycollectionname.find()

I get this error:

我收到此错误:

error: { "$err" : "not authorized for query on mydbname.request", "code" : 13 }

Any ideas what can be happening?

任何想法会发生什么?

So far I've tried adding every role I can find to the user but that hasn't helped.

到目前为止,我已经尝试向用户添加我能找到的每个角色,但这并没有帮助。

采纳答案by Sebastian

You need to assign the readrole to the user in question.

您需要将read角色分配给相关用户。

The dbAdminrole does notinclude read access on non-system collections.

dbAdmin角色并没有包括在读访问非系统集合

回答by ticofab

For anybody running into this problem against Mongo 3.0.6, I fixed by adding

对于针对 Mongo 3.0.6 遇到此问题的任何人,我通过添加

?authMode=scram-sha1

at the end of my mongodb uri.

在我的 mongodb uri 的末尾。

Here are some docs explaining the purpose of scram-sha1

这里有一些文档解释了scram-sha1的目的

回答by Flaudre

In addition to @Sebastian's answer, that means explicitely:

除了@Sebastian 的回答,这意味着明确:

Grant a Role

Grant a role using the db.grantRolesToUser() method. For example, the following operation grants the reportsUser user the read role on the accounts database:

授予角色

使用 db.grantRolesToUser() 方法授予角色。例如,以下操作授予reportsUser 用户对accounts 数据库的读取角色:

db.grantRolesToUser(
    "your_user",
    [
      { role: "read", db: "your_db" }
    ]
)

回答by Dean

I'm posting this because I had trouble finding this solution online. The problem is embarrassing but the error message and scenario make it somewhat difficult to figure out so I'm hoping to save someone some pain. My application was able to establish a database connection, start and serve static pages but every time it tried to execute a query I got this error.

我发布这个是因为我在网上找不到这个解决方案。这个问题很尴尬,但错误信息和场景让人有点难以弄清楚,所以我希望能帮到一些人。我的应用程序能够建立数据库连接,启动并提供静态页面,但每次尝试执行查询时我都会收到此错误。

MongoError: not authorized on mydb to execute command { count: "urls", query: {} }

This error was caused by a userid and password with the wrong separator

此错误是由用户 ID 和密码的分隔符错误引起的

mongodb://myuserid/[email protected]:12345/mydb [wrong]
mongodb://myuserid:[email protected]:12345/mydb [right]

While the node application was able to successfully connect to MongoDB, the incorrectly formatted URI caused the driver to skip authentication prior to issuing database commands.

虽然节点应用程序能够成功连接到 MongoDB,但格式不正确的 URI 导致驱动程序在发出数据库命令之前跳过身份验证。

Thanks and a hat tip to the people at mLab support.

感谢并感谢 mLab 支持人员。

回答by IKo

In my case helped to add ?authSource=adminto uri.

在我的情况下有助于添加?authSource=admin到 uri。

So the uri looked like this:

所以uri看起来像这样:

spring.data.mongodb.uri=mongodb://user:password@host:port/database?authSource=admin

回答by Chirag Purohit

I was getting error like

我收到错误,例如

err { MongoError: not authorized on XXXX to execute command { $eval: function(){ return db.tbl_user.find( {

err { MongoError: 未授权 XXXX 执行命令 { $eval: function(){ return db.tbl_user.find( {

For me role executeFunctions worked. Once you give role your user should look like following...

对我来说,executeFunctions 起作用了。授予角色后,您的用户应如下所示...

{ "_id" : "XXXXX", "user" : "USERXXX", "db" : "DBXXXX", "roles" : [ { "role" : "executeFunctions", "db" : "admin" }, { "role" : "dbOwner", "db" : "fryendsapp" } ] }

{ "_id": "XXXXX", "user": "USERXXX", "db": "DBXXXX", "roles" : [ { "role": "executeFunctions", "db": "admin" }, { "角色”:“dbOwner”,“db”:“fryendsapp”}]}

回答by Jaffer Pech

I had the same problem, after a lot of trial and error I solved it by adding the role "read" to my user but indicating the database as well:

我遇到了同样的问题,经过大量的反复试验,我通过向我的用户添加角色“读取”但同时指示数据库来解决它:

db.grantRolesToUser("myUserName",
[{?role:?"read",?db:?"myDataBaseName"}] );

Greetings

你好

回答by Dan Chrostowski

I was having the same problem until I realized I made a mistake. I was inheriting a mongo client pointing to a different database from a parent Perl class without realizing it. Maybe something you can check.

我遇到了同样的问题,直到我意识到我犯了一个错误。我从父 Perl 类继承了一个指向不同数据库的 mongo 客户端,而没有意识到这一点。也许你可以检查一下。