使用 pymongo 无需密码连接到 MongoDB
Connect to MongoDB without password using pymongo
我对 pymongo 和 MongoDB 的问题有点困惑。
我有一个 MongoDB 服务器,我可以使用 Studio 3T 连接它而无需任何密码(在 VPN 后面):
mongodb://mongodb-dev.my.server:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000
当我从 Python 连接到服务器时出现错误:
pymongo.errors.ServerSelectionTimeoutError: Could not reach any servers in [('mongodb-dev', 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 5.0s, Topology Description: <TopologyDescription id: 6200d97072f39326314e6916, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mongodb-dev', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongodb-dev:27017: [Errno 11001] getaddrinfo failed
Python 脚本:
import pymongo
client = pymongo.MongoClient('mongodb://mongodb-dev.my.server:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000')
db = client.get_database('feeeper')
col = db.get_collection('my_collection')
cur = col.find({}, {'body': 1})
last_task = cur.sort([('created', pymongo.DESCENDING)]).limit(1)
for t in last_task:
print(t)
更新:
Ping 到服务器工作正常:
我可以用 mongo shell 连接到服务器:
问题出在 MongoClinet
构造函数的默认参数上。构造函数有参数 directConnection
和默认值 False
— 连接到副本集:
directConnection (optional): if True, forces this client to
connect directly to the specified MongoDB host as a standalone. If false, the client connects to the entire replica set of which the given MongoDB host(s) is a part. If this is True and a mongodb+srv:// URI or a URI containing multiple seeds is provided, an exception will be raised.
当我将 directConnection
设置为 True
时,查询工作正常。
我对 pymongo 和 MongoDB 的问题有点困惑。
我有一个 MongoDB 服务器,我可以使用 Studio 3T 连接它而无需任何密码(在 VPN 后面):
mongodb://mongodb-dev.my.server:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000
当我从 Python 连接到服务器时出现错误:
pymongo.errors.ServerSelectionTimeoutError: Could not reach any servers in [('mongodb-dev', 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 5.0s, Topology Description: <TopologyDescription id: 6200d97072f39326314e6916, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mongodb-dev', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongodb-dev:27017: [Errno 11001] getaddrinfo failed
Python 脚本:
import pymongo
client = pymongo.MongoClient('mongodb://mongodb-dev.my.server:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000')
db = client.get_database('feeeper')
col = db.get_collection('my_collection')
cur = col.find({}, {'body': 1})
last_task = cur.sort([('created', pymongo.DESCENDING)]).limit(1)
for t in last_task:
print(t)
更新:
Ping 到服务器工作正常:
我可以用 mongo shell 连接到服务器:
问题出在 MongoClinet
构造函数的默认参数上。构造函数有参数 directConnection
和默认值 False
— 连接到副本集:
directConnection (optional): if True, forces this client to connect directly to the specified MongoDB host as a standalone. If false, the client connects to the entire replica set of which the given MongoDB host(s) is a part. If this is True and a mongodb+srv:// URI or a URI containing multiple seeds is provided, an exception will be raised.
当我将 directConnection
设置为 True
时,查询工作正常。