MongoLab/PyMongo connection error

I figured it out. You can do this from the python file and it will work:

connection = pymongo.MongoClient(ab123456.mlab.com, 123456)
db = connection[databasename]
db.authenticate(database_user, database_pass)

Appending /?authSource=admin helped me. Full Example:

uri = 'mongodb://username:[email protected]:27017/?authSource=admin'

client = MongoClient(uri)
db = client.test

result = db.users.find()

for document in result:
    print(document)

Version: pymongo 3.7.2