Question

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

{
        "_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:

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.

Was it helpful?

Solution

You need to assign the read role to the user in question.

The dbAdmin role does not include read access on non-system collections.

OTHER TIPS

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

?authMode=scram-sha1

at the end of my mongodb uri.

Here are some docs explaining the purpose of scram-sha1

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

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(
    "your_user",
    [
      { role: "read", db: "your_db" }
    ]
)

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

mongodb://myuserid/mypassword@ds112345.mlab.com:12345/mydb [wrong]
mongodb://myuserid:mypassword@ds112345.mlab.com: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.

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

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

So the uri looked like this:

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

I was getting error like

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

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

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top