无法使用 PyMongo 连接 Atlas 集群
Unable to connect with Atlas cluster using PyMongo
一段时间以来,我一直在尝试让 Pymongo 与我的免费 atlas 集群一起工作,但只是遇到了如下错误:
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 60b3d56c099a114d04aa3b66, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('sandbox-shard-00-00.r2jpi.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('sandbox-shard-00-01.r2jpi.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('sandbox-shard-00-02.r2jpi.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>]>
I am not sure what is causing this. Yes, my IP is whitelisted, and yes my Atlas database has a user with proper privileges. All the functions which involve communicating with the server seems to be failing.
如果我只是 运行:
import pymongo
from pymongo import MongoClient
import datetime
client = MongoClient(
'mongodb://<username>:<pw>@sandbox.r2jpi.mongodb.net/test?authSource=admin&replicaSet=atlas-jexqkj-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true')
db = client.sample_mflix
print(db)
我得到:
Database(MongoClient(host=['sandbox-shard-00-02.r2jpi.mongodb.net:27017', 'sandbox-shard-00-00.r2jpi.mongodb.net:27017', 'sandbox-shard-00-01.r2jpi.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, retrywrites=True, w='majority', authsource='admin', replicaset='atlas-jexqkj-shard-0', ssl=True), 'sample_mflix')
OS: Windows 10
Python版本:3.9.2
PyMongo 版本:3.9.0
下面是我正在尝试的代码 运行(导致 serverTimeOutError 的代码)
import pymongo
from pymongo import MongoClient
import datetime
client = MongoClient(
'mongodb://<username>:<pw>@sandbox.r2jpi.mongodb.net/test?authSource=admin&replicaSet=atlas-jexqkj-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true')
db = client.sample_mflix
trying = db.trying
personDocument = {
"name": {"first": "Alan", "last": "Turing"},
"birth": datetime.datetime(1912, 6, 23),
"death": datetime.datetime(1954, 6, 7),
"contribs": ["Turing machine", "Turing test", "Turingery"],
"views": 1250000
}
trying.insert_one(personDocument)
Atlas 集群使用 SRV 记录来确定每个分片/副本集的 DNS 记录。
因此您需要使用以 mongodb+srv://
.
开头的连接字符串进行连接
一段时间以来,我一直在尝试让 Pymongo 与我的免费 atlas 集群一起工作,但只是遇到了如下错误:
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 60b3d56c099a114d04aa3b66, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('sandbox-shard-00-00.r2jpi.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('sandbox-shard-00-01.r2jpi.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('sandbox-shard-00-02.r2jpi.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>]>
I am not sure what is causing this. Yes, my IP is whitelisted, and yes my Atlas database has a user with proper privileges. All the functions which involve communicating with the server seems to be failing.
如果我只是 运行:
import pymongo
from pymongo import MongoClient
import datetime
client = MongoClient(
'mongodb://<username>:<pw>@sandbox.r2jpi.mongodb.net/test?authSource=admin&replicaSet=atlas-jexqkj-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true')
db = client.sample_mflix
print(db)
我得到:
Database(MongoClient(host=['sandbox-shard-00-02.r2jpi.mongodb.net:27017', 'sandbox-shard-00-00.r2jpi.mongodb.net:27017', 'sandbox-shard-00-01.r2jpi.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, retrywrites=True, w='majority', authsource='admin', replicaset='atlas-jexqkj-shard-0', ssl=True), 'sample_mflix')
OS: Windows 10
Python版本:3.9.2
PyMongo 版本:3.9.0
下面是我正在尝试的代码 运行(导致 serverTimeOutError 的代码)
import pymongo
from pymongo import MongoClient
import datetime
client = MongoClient(
'mongodb://<username>:<pw>@sandbox.r2jpi.mongodb.net/test?authSource=admin&replicaSet=atlas-jexqkj-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true')
db = client.sample_mflix
trying = db.trying
personDocument = {
"name": {"first": "Alan", "last": "Turing"},
"birth": datetime.datetime(1912, 6, 23),
"death": datetime.datetime(1954, 6, 7),
"contribs": ["Turing machine", "Turing test", "Turingery"],
"views": 1250000
}
trying.insert_one(personDocument)
Atlas 集群使用 SRV 记录来确定每个分片/副本集的 DNS 记录。
因此您需要使用以 mongodb+srv://
.