MongoLab/PyMongo connection error

后端 未结 3 1787
傲寒
傲寒 2021-02-04 13:33

If I run in the shell:

mongo ds0219xx.mlab.com:219xx/dbname -u user -p pass

It works and allows me to connect to the database and pull informat

相关标签:
3条回答
  • 2021-02-04 13:40

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

    0 讨论(0)
  • 2021-02-04 13:46

    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)
    
    0 讨论(0)
  • 2021-02-04 13:49

    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

    0 讨论(0)
提交回复
热议问题