cybertextron
cybertextron

Reputation: 10981

Connecting to a MongoDB replica set - pymongo

So I have a cluster of 3 mongo replica sets. The primary (master) is at rs1, while rs2 and rs3 are secondaries. However, the primary may change, for example, rs2 may be the primary at any given time due to some configuration issue. Therefore, I need to be able to connect to the appropriate replica set. Let's say they are at rs1=10.1.1.1, rs2=10.1.1.2 and rs3=10.1.1.3. Here's how I'm connecting to Mongo:

mongoserver_uri = "mongodb://{0}:{1}@{2}:{3}/admin".format(
                    username, password, host, port)
self.log.info("------- Mongo Server URI: %s --------" % mongoserver_uri)
self.client = pymongo.MongoClient(host=mongoserver_uri, ssl=True,
                                  ssl_cert_reqs=ssl.CERT_NONE)

However if, like explained, the replica set primary changes to let's say rs3 then I would be unable to connect to Mongo:

  File "/home/ubuntu/myproject/mongodb.py", line 165, in clear_collections
    collection.remove()
  File "/usr/local/lib/python2.7/site-packages/pymongo/collection.py", line 2258, in remove
    return self._delete(sock_info, spec_or_id, multi, write_concern)
  File "/usr/local/lib/python2.7/site-packages/pymongo/collection.py", line 916, in _delete
    codec_options=self.codec_options)
  File "/usr/local/lib/python2.7/site-packages/pymongo/pool.py", line 218, in command
    self._raise_connection_failure(error)
  File "/usr/local/lib/python2.7/site-packages/pymongo/pool.py", line 346, in _raise_connection_failure
    raise error
pymongo.errors.NotMasterError: not master

How can I specify the other replica sets to the MongoClient?

Upvotes: 2

Views: 6801

Answers (3)

Madhur Gupta
Madhur Gupta

Reputation: 114

dbconnect = MongoClient('mongodb://mongos01:27017,mongos02:27017,mongos03:27017/?replicaSet=seplicaSetName',username='username',password='password',authSource='dbname', authMechanism='SCRAM-SHA1')

Upvotes: 0

michael_j_ward
michael_j_ward

Reputation: 4579

From the docs

A connection to a replica set can be made using the MongoClient() constructor, specifying one or more members of the set, along with the replica set name.

mongoserver_uri = "mongodb://{0}:{1}@{2}:{3}/admin".format(
                    username, password, host, port)
self.log.info("------- Mongo Server URI: %s --------" % mongoserver_uri)
self.client = pymongo.MongoClient(host=mongoserver_uri, ssl=True,
                                  ssl_cert_reqs=ssl.CERT_NONE,
                                  replicaset='name_of_set')

As long as the server you connect to is online at initially, the MongoClient will find all members of the replicaSet and automatically attempt to find a new primary on failover.

Upvotes: 4

ThrowsException
ThrowsException

Reputation: 2636

In your MongoClient you need to tell pymongo that you are connecting to a replica set so it can discover all the members in the set https://api.mongodb.com/python/current/examples/high_availability.html#id1

Upvotes: 2

Related Questions