MongoDB not authorized for query - code 13

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" }
    ]
)

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

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


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/[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.

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