pymongo replication secondary readreference not work

走远了吗. 提交于 2019-12-24 03:50:49

问题


we have MongoDB 2.6 and 2 Replica Set, and we use pymongo driver and connect Mongo Replicat Set with the following url

mongodb://admin:admin@127.0.0.1:10011:127.0.0.1:10012,127.0.0.1:10013/db?replicaSet=replica

with python code

from pymongo import MongoClient
url = 'mongodb://admin:admin@127.0.0.1:10011:127.0.0.1:10012,127.0.0.1:10013/db?replicaSet=replica'
db = 'db'
db = MongoClient(
    url,
    readPreference='secondary',
    secondary_acceptable_latency_ms=1000,
)[db]
db.test.find_one()
# more read operations

but it turns out that the connection didn't read anything from secondary replicat set, no connection log could be found in mongo log on these 2 secondary replica set


回答1:


Try to configure a ReplicaSetConnection instead. For instance:

>>> db = ReplicaSetConnection("morton.local:27017", replicaSet='foo').test
>>> from pymongo import ReadPreference
>>> db.read_preference = ReadPreference.SECONDARY


来源:https://stackoverflow.com/questions/30542997/pymongo-replication-secondary-readreference-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!