简体   繁体   English

Pymongo pymongo.errors.ServerSelectionTimeoutError 使用示例代码时

[英]Pymongo pymongo.errors.ServerSelectionTimeoutError when using example code

I am trying to run very straightforward code to figure out how to use pymongo with the MongoDB Atlas Cloud.我正在尝试运行非常简单的代码来弄清楚如何将 pymongo 与 MongoDB Atlas Cloud 一起使用。

Here is the example code这是示例代码

import pymongo

client = pymongo.MongoClient("mongodb+srv://{myusername}:{mypassword}@cluster0-uywu8.mongodb.net/test?retryWrites=true&w=majority")
db = client.BroadwayMatch
print(db)
collection = db.Artists
print(collection)

print(collection.insert_one({'x': 1}))

BroadwayMatch and Artists are existing databases and collections that I was able to insert to last week, I am not sure what changed. BroadwayMatch 和 Artists 是我上周能够插入的现有数据库和 collections,我不确定发生了什么变化。 It seems to be connecting to the database and collection successfully, but for some reason is unable to read or write to it.它似乎成功连接到数据库和集合,但由于某种原因无法读取或写入它。 All attributes of collections can be accessed, but all methods result in a ServerSelectionTimeoutError. collections 的所有属性都可以访问,但所有方法都会导致 ServerSelectionTimeoutError。 Here is the output from this snippet这是此片段中的 output

Database(MongoClient(host=['cluster0-shard-00-01-uywu8.mongodb.net:27017', 'cluster0-shard-00-00-uywu8.mongodb.net:27017', 'cluster0-shard-00-02-uywu8.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authsource='admin', replicaset='Cluster0-shard-0', ssl=True, retrywrites=True, w='majority'), 'BroadwayMatch')
Collection(Database(MongoClient(host=['cluster0-shard-00-01-uywu8.mongodb.net:27017', 'cluster0-shard-00-00-uywu8.mongodb.net:27017', 'cluster0-shard-00-02-uywu8.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authsource='admin', replicaset='Cluster0-shard-0', ssl=True, retrywrites=True, w='majority'), 'BroadwayMatch'), 'Artists')
Traceback (most recent call last):
  File "C:\Python37\Spotify-Match\mongotest.py", line 10, in <module>
    print(collection.insert_one({'x': 1}))
  File "C:\Python37\lib\site-packages\pymongo\collection.py", line 700, in insert_one
    session=session),
  File "C:\Python37\lib\site-packages\pymongo\collection.py", line 614, in _insert
    bypass_doc_val, session)
  File "C:\Python37\lib\site-packages\pymongo\collection.py", line 602, in _insert_one
    acknowledged, _insert_command, session)
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1279, in _retryable_write
    with self._tmp_session(session) as s:
  File "C:\Python37\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1611, in _tmp_session
    s = self._ensure_session(session)
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1598, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1551, in __start_session
    server_session = self._get_server_session()
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1584, in _get_server_session
    return self._topology.get_server_session()
  File "C:\Python37\lib\site-packages\pymongo\topology.py", line 434, in get_server_session
    None)
  File "C:\Python37\lib\site-packages\pymongo\topology.py", line 200, in _select_servers_loop
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed

I'm not sure what I am doing wrong, can anyone help?我不确定我做错了什么,有人可以帮忙吗?

Reasons you might not be able to connect to an Atlas server:您可能无法连接到 Atlas 服务器的原因:

  • Your white list has not enable your current IP address您的白名单没有启用您当前的 IP 地址
  • you are using the wrong username and/or password.您使用了错误的用户名和/或密码。 In your case it looks like your fString is missing an f at the start.在您的情况下,您的 fString 似乎在一开始就缺少 f 。

When diagnosing these conditions cutting and pasting the MongoDB Atlas connection string (see below) for the MongoDB shell or MongoDB Compass can often expose username and/or password errors. When diagnosing these conditions cutting and pasting the MongoDB Atlas connection string (see below) for the MongoDB shell or MongoDB Compass can often expose username and/or password errors.

MongoDB Shell 的 MongoDB Atlas Connection 屏幕对话框的屏幕主机

Either your mongo server is not exposed otherwise it is not in the default port.要么您的 mongo 服务器未公开,否则它不在默认端口中。 Try following:尝试以下操作:

import pymongo
client = pymongo.MongoClient("mongodb://uname:pass@ip:port/")
db = client['BroadwayMatch']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pymongo.errors.ServerSelectionTimeoutError - pymongo.errors.ServerSelectionTimeoutError 烧瓶 - pymongo.errors.ServerSelectionTimeoutError - Flask - pymongo.errors.ServerSelectionTimeoutError 尝试连接到 CosmosDB 数据库时出现 pymongo.errors.ServerSelectionTimeoutError - Getting pymongo.errors.ServerSelectionTimeoutError when attempting to connect to a CosmosDB database pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas - pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas 连接到 mongo 图集给出 pymongo.errors.ServerSelectionTimeoutError: localhost:27017 - Connecting to mongo atlas gives pymongo.errors.ServerSelectionTimeoutError: localhost:27017 pymongo.errors.ServerSelectionTimeoutError:本地主机:27017:[Errno 61]连接被拒绝 - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused pymongo.errors.ServerSelectionTimeoutError 错误与 MongoDB 图集和 Flask - pymongo.errors.ServerSelectionTimeoutError error with MongoDB atlas and Flask 烧瓶 Web 服务中的 Mongodb 错误 [pymongo.errors.ServerSelectionTimeoutError] - Mongodb error [pymongo.errors.ServerSelectionTimeoutError] in flask web-service pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] 连接被拒绝 - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused pymongo pymongo.errors.ServerSelectionTimeoutError:hour must be in 0..23, Timeout: 30s, Topology Description: - pymongo pymongo.errors.ServerSelectionTimeoutError:hour must be in 0..23, Timeout: 30s, Topology Description:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM