pymongo MongoClient连接到ReplicaSet [英] pymongo MongoClient connect to ReplicaSet

查看:969
本文介绍了pymongo MongoClient连接到ReplicaSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采用了pymongo的MongoClient类来连接到具有三个节点的副本集,其中1个是主节点,2个是辅助节点.代码段如下:

I adopted pymongo's MongoClient class to do connect to a replicaset which has three node, 1 primary 2 secondary. The code snippet as following:

c = MongoClient([secondary1_hostname, secondary2_hostname], replicaSet='rs0')

当检查三个mongod的日志时,我发现总是与主要主机建立了连接,但是其他2个次要主机未收到来自客户端的连接请求,或者立即断开了连接.似乎客户端首先到达一个辅助节点,获得了主节点地址,然后断开了连接,并建立了到主节点的长期连接.

When check the three mongod's log, I found there is always a connection created to the primary host, but other 2 secondary not received the connection request from client or got connection immediately disconnected. Seems the client first reached one secondary got the primary address then dropped the connection and created long-term connection to primary.

但是,当我使用MongoReplicaSetClient类时,使用以下代码sinppet:

However, when I use MongoReplicaSetClient class, with the follwing code sinppet:

c = MongoReplicaSetClient(secondary1_name, replicaSet='rs0')

从mongod的日志文件获得的每个副本集成员始终创建3个连接.

There are always 3 connection created to each replica set member, got from the mongod's log file.

那么,为什么MongoClient的行为总是仅创建与主要对象的连接?我阅读了PyMongo的手册,但没有找到答案.任何建议表示赞赏.

So, why the behavior of MongoClient is always only create connection to the primary? I read the manual of PyMongo, but didn't find the answer. Any suggestion is appreciated.

推荐答案

MongoClient仅用于单个连接,当与MongoD通话时,它将选择数据库列表中的最后一个.添加replicaSet pymongo时,它将验证它连接到的副本集与此名称匹配.表示指定的主机是种子列表,并且pymongo应该尝试查找该集合的所有成员,然后它将连接到主要"节点.

MongoClient is for single connections only, and when speaking to MongoD it will chose the last in the list of databases. When adding a replicaSet pymongo it will verify that the replica set it connects to matches this name. Implies that the hosts specified are a seed list and pymongo should attempt to find all members of the set, then it will connect to the Primary node.

MongoClient接受多个主机的另一个原因是为了处理Mongos和高可用性:

Another reason MongoClient accepts multiple hosts is for handling Mongos and high availability: http://api.mongodb.org/python/current/examples/high_availability.html#high-availability-and-mongos MongoClient also handles replicaset configurations for when speaking to a replicaSet via Mongos.

MongoReplicaSetClient专门用于副本集连接,它尝试查找集合的所有成员.它还启动了副本集监控器,使它可以快速响应副本集配置中的更改.

MongoReplicaSetClient is specifically for replicaset connections, it attempts to find all members of the set. It also launches the replica-set monitor, which allows it to quickly respond to changes in replica set configuration.

这篇关于pymongo MongoClient连接到ReplicaSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆