我正在运行一个Kubernetes MongoDB ReplicaSets群集。
我已经很好地初始化了复制品,一切都在彼此同步。
members" : [
{
"_id" : 1,
"host" : "10.244.7.3:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "10.244.6.4:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 3,
"host" : "10.244.7.5:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
]
但是当我尝试使用pymongo使用私有公开的ip地址(在172.20.x
范围内)进行连接时,运行查询会尝试使用mongo复制服务器提供的ip连接到replicaset
import pymongo
from pymongo import MongoClient
mongo_uri = "mongodb://172.20.0.19:31892,172.20.0.80:31892,172.20.0.49:31892,172.20.0.147:31892/a-dev?replicaSet=rs0"
c = MongoClient(mongo_uri)
c['test_db'].tab.find_one()
# Returns a
*** pymongo.errors.ServerSelectionTimeoutError: 10.244.6.4:27017: timed out,10.244.7.5:27017: timed out,10.244.7.3:27017: timed out
这表明pymongo正在使用replicaset配置提供的值,而不是使用我定义的ip地址。无论如何都要禁用此默认行为吗?