所以我有一個3 mongo副本集的集羣。主(主)位於rs1,而rs2和rs3是輔助。 但是,主節點可能會發生變化,例如,由於某些配置問題,rs2可能是任何給定時間的主節點。因此,我需要能夠連接到適當的副本集。 假設他們在rs1=10.1.1.1
,rs2=10.1.1.2
和rs3=10.1.1.3
。下面是我如何連接到蒙戈:連接到MongoDB副本集 - pymongo
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)
然而,如果像解釋,副本集主要的變化讓我們說rs3
那麼我將無法連接到蒙戈:
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
哪有我指定其他副本集到MongoClient
?