pymongo ReplicaSet用户名密码

时间:2017-02-08 07:09:57

标签: mongodb pymongo mongodb-replica-set

我使用以下语法建立与副本集的连接:

from urllib.parse import quote
mongo_client = pymongo.MongoClient(host=[
    quote('username:password@ipaddress1:27017'),
    quote('username:password@ipaddress2@10.0.5.65:27017'),
    quote('username:password@ipaddress3@10.0.2.176:27017')],
                                    connect=False,
replicaset="enterprise")

但是,当我尝试插入这样的文档时:

db = mongo_client.test
db.test.insert_one({"test": "test"})

我得到一个pymongo ServerSelectionTimeOutError:名称或服务未知

我做错了什么?

1 个答案:

答案 0 :(得分:0)

请改为:

MongoClient('mongodb://username:password@ipaddress1,ipaddress2,ipaddress3/?replicaSet=enterprise')

请参阅连接字符串docs:

https://docs.mongodb.com/manual/reference/connection-string/#standard-connection-string-format