How to resolve pymongo authentication failure error?

808 Views Asked by At

I am trying to fetch data from my mongodb but it fails with:

pymongo.errors.OperationFailure: Authentication failed.

It is weird because when I am trying to authenticate with same credentials through robo3T (MongoDB GUI) it is able to connect easily.

Through code it is like:

client = MongoClient(host=host,
                         port=int(port),
                         username=username_admin_db,
                         password=password_admin_db,
                         authSource=authSource_admin_db,
                         maxPoolSize=15,
                         MaxIdleTimeMS=120000
                        )
db_handle = client[database_name]

for i in db_handle[collection_name].find({'account_id': "1234"}):
    print(i)

In mongod.conf:

security:
     authorization: enabled

It is also weird because through the same code I am able to authenticate to another db on a different server with same credentials and same db name.

Versions:

Python 3.6.10
MongoDB 3.4
pymongo 3.5.1
Django  1.11.7
0

There are 0 best solutions below