從mongoDB副本集中刪除主機但未更改主機使用該副本集傳遞給mongo_client(或MongoReplicaSetClient)的主機字符串在重新啓動服務時似乎破壞了pymongo連接。此異常升高是:pymongo在副本集組合更改時無法連接
pymongo.errors.ServerSelectionTimeoutError:主機4:27017:[錯誤-2]名稱或不知道的服務...
問題可以蒸餾如下:
hosts1 = "host1, host2, host3, host4" # where host1 and host2 are not available anymore
hosts2 = "host3, host4" # only has valid hosts
hosts3 = ["host1", "host2", "host3", "host4"] # expressed as a list
client = MongoClient(hosts1, 27017, replicaset="rs0")
db = client['admin']
db.authenticate('user', 'pass')
因此,該腳本將失敗,並與host1,但與host2和host3,即。
client = MongoClient(hosts2, 27017, replicaset="rs0") # works
或:
client = MongoClient(hosts3, 27017, replicaset="rs0") # works
的問題與此是,直到重新啓動該服務這個問題不會變得明顯,副本集成員更改後可能很多後來發生。
它與hosts2一起使用的事實表明所使用的主機格式字符串是有效的。那麼爲什麼當重新啓動服務時第一個失敗呢?