Search code examples
mongodbpymongomongodb-replica-set

pymongo ReplicaSet username password


I'm using the following syntax to establish a connection to my replica set:

from urllib.parse import quote
mongo_client = pymongo.MongoClient(host=[
    quote('username:password@ipaddress1:27017'),
    quote('username:password@ipaddress2@10.0.5.65:27017'),
    quote('username:password@ipaddress3@10.0.2.176:27017')],
                                    connect=False,
replicaset="enterprise")

However when I try to insert a document like this:

db = mongo_client.test
db.test.insert_one({"test": "test"})

I get a pymongo ServerSelectionTimeOutError: Name or service not known

What am I doing wrong?


Solution

  • Do this instead:

    MongoClient('mongodb://username:password@ipaddress1,ipaddress2,ipaddress3/?replicaSet=enterprise')
    

    See the connection string docs:

    https://docs.mongodb.com/manual/reference/connection-string/#standard-connection-string-format