简体   繁体   English

连接到 MongoDB 副本集 - pymongo

[英]Connecting to a MongoDB replica set - pymongo

So I have a cluster of 3 mongo replica sets.所以我有一个由 3 个 mongo 副本集组成的集群。 The primary (master) is at rs1, while rs2 and rs3 are secondaries.主要(主)在 rs1,而 rs2 和 rs3 是次要的。 However, the primary may change, for example, rs2 may be the primary at any given time due to some configuration issue.但是,主节点可能会更改,例如,由于某些配置问题,rs2 可能在任何给定时间成为主节点。 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 .假设它们位于rs1=10.1.1.1rs2=10.1.1.2rs3=10.1.1.3 Here's how I'm connecting to Mongo:这是我连接到 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:但是,如果像解释的那样,副本集主要更改为rs3那么我将无法连接到 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 ?如何将其他副本集指定给MongoClient

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.可以使用 MongoClient() 构造函数建立到副本集的连接,指定集合的​​一个或多个成员以及副本集名称。

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.只要您连接的服务器最初在线,MongoClient 就会查找 replicaSet 的所有成员,并在故障转移时自动尝试查找新的主服务器。

在您的 MongoClient 中,您需要告诉 pymongo 您正在连接到副本集,以便它可以发现集合中的所有成员https://api.mongodb.com/python/current/examples/high_availability.html#id1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM