MongoDB错误:Could not find host matching read preference { mode: “primary“ } for set shard1
【代码】MongoDB错误:Could not find host matching read preference { mode: "primary" } for set shard1。
·
之前查了很多文章,发现重启以后也没什么用,这里为大家提供一种可能出现的情况的解决办法:
- 出现这个问题时,大家首先看mongodb连接是否将readPreference设置为secondary,如果有的话,可以删除掉这个配置,或者说变更为primary。
注:readPreference 主要控制客户端driver从副本集(Replica Set)读数据的时候如何路由。
下图为本人uri修改过程,仅供参考:
class MongoClient(object):
def __init__(self, mongo_uri='127.0.0.1:27017'):
self.mongo_uri = 'mongodb://{}:{}@{}/admin?connectTimeoutMS=300&ssl=false&minPoolSize=2&maxPoolSize=10&readConcernLevel=majority&readPreference=secondary&authMechani
sm=SCRAM-SHA-1&localThresholdMS=30'.format(username, password, mongo_uri)
self.client = pymongo.MongoClient(self.mongo_uri, connect=True, unicode_decode_error_handler='ignore')
改为
class MongoClient(object):
def __init__(self, mongo_uri='127.0.0.1:27017'):
self.mongo_uri = 'mongodb://{}:{}@{}/admin?connectTimeoutMS=300&ssl=false&minPoolSize=2&maxPoolSize=10&readConcernLevel=majority&authMechani
sm=SCRAM-SHA-1&localThresholdMS=30'.format(username, password, mongo_uri)
self.client = pymongo.MongoClient(self.mongo_uri, connect=True, unicode_decode_error_handler='ignore')
更多推荐
已为社区贡献2条内容
所有评论(0)