[英]Pymongo : how to ensure connection and reading from only desired host (especially SECONDARY) in a replica set
Background 背景
mongoDB production cluster (not sharded) 3 hosts. mongoDB生产集群(未分片)3个主机。 They belong to replica set rs0 它们属于副本集rs0
I am trying to write a python script using pymongo . 我正在尝试使用pymongo编写python脚本。 This being a production cluster, I want the script to read only from H3 secondary replica set so as to not burden primary and keep it free for its regular activities. 这是一个生产集群,我希望脚本仅从H3辅助副本集中读取,以免增加主负载并使其正常活动自由。 The data will be fetched from a collection called "devices" in the batches of 2000 documents . 将从2000个批次的文档中称为“设备”的集合中获取数据。 This should further reduce read operations overhead. 这将进一步减少读取操作的开销。
Relevant Code 相关代码
client = MongoClient('mongodb://H3-hostname:27017/', replicaset='rs0', readPreference='secondary')
.
.
.
.
.
devices = devices_collection.find({"status" : "ACTIVE"},{"key" : 1, "username" : 1}, sort = [("key", pymongo.ASCENDING)]).batch_size(2000)
Also tried creating the client in a little different way by mentioning all the hosts in the cluster as below 还尝试通过提及群集中的所有主机,以稍微不同的方式创建客户端,如下所示
client = MongoClient('mongodb://H1-Hostname:27017, H2-Hostname:27017, H3-hostname:27017/?replicaset=rs0', readPreference='secondary')
Problem 问题
As per my understanding, the above client will read ONLY from the secondary replica set. 按我的理解,上述客户将只能从辅助副本集读取。 But when I run the script and look at the cloud manager, H1 which is primary shows page faults increased from ~15-20 to ~600 with a sharp jump. 但是,当我运行脚本并查看云管理器时,主要的H1显示页面错误从15-20急剧增加到600。
Environment 环境
I also posted this problem to mongo-user group, where it was answered. 我还将此问题发布到mongo用户组,在那里得到了回答。
First of all, the version of pymongo was wrong. 首先,pymongo的版本是错误的。 It was 2.8, at least the one which python27 was using 它是2.8,至少是python27正在使用的那个
$ python27
>>> import pymongo
>>> pymongo.version
'2.8'
and 和
$ python
>>> import pymongo
>>> pymongo.version
'3.0.3'
The appropriate syntax for that was 合适的语法是
MongoClient('mongodb://H3-Hostname:27017/?readPreference=secondary')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.