I'm gettting a "not authorized for query" error through PyMongo even though I am authorized

2,416 views
Skip to first unread message

Jon Crowell

unread,
Nov 8, 2014, 10:13:10 AM11/8/14
to mongod...@googlegroups.com
So I'm trying to use PyMongo to connect to and manipulate a Mongo database.

I have the following code:

 
from pymongo import MongoClient

def mongo_uri():
   
return 'mongodb://username:pass...@somewhere.mongolayer.com:10011/my_app'

def my_db():
   
return MongoClient(mongo_uri()).my_db



When I call `db = my_db()` I get a `pymongo.database.Database` object with the correct name. So something is working correctly.

However, when I try to call `db.show_collections()` I get an error:

    OperationFailure: database error: not authorized for query on my_db.system.namespaces

What am I doing wrong?

Asya Kamsky

unread,
Nov 8, 2014, 7:55:40 PM11/8/14
to mongodb-user
What permissions do you have on the database? Btw, what version is this?

Asya
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> http://www.mongodb.org/about/support/.
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/94acd741-e0bc-47b4-b367-1c5a42171791%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jon Crowell

unread,
Nov 10, 2014, 10:10:56 AM11/10/14
to mongod...@googlegroups.com
I was using version 2.4.10 but just upgraded to version 2.6.5.  I'm running Mongo in the cloud using Compose.io -- it was an addon through Heroku.

I don't know what permissions I have on the database.  I guess I would have assumed I had all permissions, since I'm just connecting through the credentials that were set up for me when I created the addon in Heroku.

I can connect using "mongo" at the terminal command line as well, but I get the "not authorized" error when I try to run commands such as "show collections" or "show users".

I can run the command "use my_db" successfully, but apparently this is slightly misleading, since I can type anything at all in place of "my_db" and it will report that it is now using that db.

Basically, I can do nothing except connect with the right name and password.  Giving the wrong name and password prevents me from connecting entirely, so I know that at least something is going through when I use the right name and password.  From that point on, however, I can't run any command except "help".





On Saturday, November 8, 2014 7:55:40 PM UTC-5, Asya Kamsky wrote:
What permissions do you have on the database?   Btw, what version is this?

Asya


On Sat, Nov 8, 2014 at 3:13 PM, Jon Crowell <jon.c...@imagineeasy.com> wrote:
> So I'm trying to use PyMongo to connect to and manipulate a Mongo database.
>
> I have the following code:
>
>
> from pymongo import MongoClient
>
> def mongo_uri():
>     return

Jon Crowell

unread,
Nov 10, 2014, 2:16:08 PM11/10/14
to mongod...@googlegroups.com
Nevermind -- I didn't realize that my database was given a different name in my production cloud instance compared to my dev instance :(

Gerald Rich

unread,
Dec 9, 2015, 8:26:13 PM12/9/15
to mongodb-user
Hi, I'm running into the same issue. How did you find the name of the database and then change it on compose.io?

thomas....@teamaol.com

unread,
Oct 19, 2016, 11:40:40 AM10/19/16
to mongodb-user
I was trying out MongoDB and ran into same issue ...
For this I was setting up Mongo DB with Docker for these tests:

docker run --name=mongodb -d -p 27017:27017 -p 28017:28017 -e MONGODB_USER="test" -e MONGODB_PASS="test" -e MONGODB_DATABASE="testdb" tutum/mongodb

If I use the MongoClient now with "mongodb://test:test@localhost:27017/" then the authorization fails
But it worked fine for me doing it following way:

from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017/')
# create database
testdb = client.testdb
if testdb.authenticate('test', 'test'):
    pass

But now I cannot access the admin interface anymore opening following url http://localhost:28017. I cannot login with "test" / "test"
since the database is now testdb instead of admin (and docker logs doesn't help too). Just my findings after my first try.

Regards,
Thomas


On Saturday, November 8, 2014 at 4:13:10 PM UTC+1, Jon Crowell wrote:
So I'm trying to use PyMongo to connect to and manipulate a Mongo database.

I have the following code:

 
from pymongo import MongoClient

def mongo_uri():

   
return 'mongodb://username:password@somewhere.mongolayer.com:10011/my_app'

def my_db():
   
return MongoClient(mongo_uri()).my_db

Bernie Hackett

unread,
Oct 20, 2016, 11:54:02 AM10/20/16
to mongodb-user
Change this:

client = MongoClient('mongodb://test:test@localhost:27017/')

To this:

client = MongoClient('mongodb://test:test@localhost:27017/testdb')

You have to tell MongoClient what database the username and password are for. It uses admin by default.

> But now I cannot access the admin interface anymore opening following url http://localhost:28017.

The "admin interface" is deprecated:


It also doesn't support the current default authentication mechanism, SCRAM-SHA-1:

Reply all
Reply to author
Forward
0 new messages