pymongo 复制辅助读取引用不起作用

pymongo replication secondary readreference not work

我们有 MongoDB 2.6 和 2 副本集,我们使用 pymongo 驱动程序并将 Mongo 副本集与以下 url

连接

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

使用 python 代码

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

但事实证明连接没有从辅助副本集读取任何内容,在这 2 个辅助副本集上的 mongo 日志中找不到连接日志

尝试配置 ReplicaSetConnection。例如:

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