python - 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. belong replica set rs0
- h1 - primary
- h2 - secondary
- h3 - secondary
i trying write python script using pymongo. being production cluster, want script read h3 secondary replica set not burden primary , keep free regular activities. data fetched collection called "devices" in batches of 2000 documents. 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 client in little different way mentioning hosts in cluster below
client = mongoclient('mongodb://h1-hostname:27017, h2-hostname:27017, h3-hostname:27017/?replicaset=rs0', readpreference='secondary') problem
as per understanding, above client read only secondary replica set. when run script , @ cloud manager, h1 primary shows page faults increased ~15-20 ~600 sharp jump.
environment
- mongo db 3.0.3 (mmapv1 storage engine)
- python 2.7.9
- pymongo 3.0.3
- os amazon linux
i posted problem mongo-user group, answered.
first of all, version of pymongo wrong. 2.8, @ least 1 python27 using
$ python27 >>> import pymongo >>> pymongo.version '2.8' and
$ python >>> import pymongo >>> pymongo.version '3.0.3' the appropriate syntax was
mongoclient('mongodb://h3-hostname:27017/?readpreference=secondary')
Comments
Post a Comment