pythonmongodbflaskpymongomongokit

How to authenticate to a remote db host with MongoKit?


I am attempting to connect and authenticate to a remote database host (dotcloud, mongolabs, etc) using MongoKit within Flask. Connecting to the server seems to work fine. However I am unable to authenticate to the database. Presumably this should work:

from mongokit import Connection
connection = Connection(my_remote_host, my_remote_port)
connection.my_database.authenticate(my_admin_user, my_admin_password)

the call to authenticate() returns True, yet subsequent calls to fetch data throw:

OperationFailure: database error: unauthorized db

Anyone know what might be happening here?


Solution

  • This is likely due to the current behavior of authenticate() in pymongo. Pymongo doesn't cache authentication credentials between threads, so each thread must authenticate individually. See the note in the pymongo documentation about using authenticate() in a multi-threaded environment.