pymongo auth failed in python script

后端 未结 4 2079
不知归路
不知归路 2020-12-31 00:44

I have installed mongodb and enabled auth. and its working find. I can connect it from remote notebook using robomongo application:

Host: SERVER_IP
PORT: 270         


        
4条回答
  •  一生所求
    2020-12-31 01:11

    If you've tried the above answers and you're still getting an error:

    pymongo.errors.OperationFailure: Authentication failed.
    

    There's a good chance you need to add ?authSource=admin to the end of your uri.

    Here's a working solution that I'm using with MongoDB server version 4.2.6 and MongoDB shell version v3.6.9.

    from pymongo import MongoClient
    
    # Replace these with your server details
    MONGO_HOST = "XX.XXX.XXX.XXX" 
    MONGO_PORT = "27017"
    MONGO_DB = "database"
    MONGO_USER = "admin"
    MONGO_PASS = "pass"
    
    uri = "mongodb://{}:{}@{}:{}/{}?authSource=admin".format(MONGO_USER, MONGO_PASS, MONGO_HOST, MONGO_PORT, MONGO_DB)
    client = MongoClient(uri)
    

    Similar fix for command line is adding --authenticationDatabase admin

提交回复
热议问题