MongoLab/PyMongo connection error

こ雲淡風輕ζ 提交于 2019-12-03 01:18:00

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)
Tim Schäfer

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

uri = 'mongodb://username:password@r1.example.net:27017/?authSource=admin'

client = MongoClient(uri)
db = client.test

result = db.users.find()

for document in result:
    print(document)

Version: pymongo 3.7.2

You need to authenticate after selecting the database for mongo < 4.0

The answer given by @Derek Schuster was correct. Just to make it more clear.

DB_NAME = database_name  
DB_HOST = dsxxxxxx.mlab.com
DB_PORT = 12345
DB_USER = user 
DB_PASS = pass

connection = MongoClient(DB_HOST, DB_PORT)
db = connection[DB_NAME]
db.authenticate(DB_USER, DB_PASS)

Hope it helps. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!