![](/img/trans.png)
[英]pymongo.errors.ServerSelectionTimeoutError error with MongoDB atlas and Flask
[英]pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas
问题是,每当(我已经尝试过 3 次)我在 MongoDB Atlas 上创建一个新集群并使用 PyMongo 在 Python 中连接到它时,前 3-4 次它会无痛地连接,但在一段时间后或休息后,当我重新-连接它会吐出以下错误:
pymongo.errors.ServerSelectionTimeoutError: ac-0c87gnx-shard-00-02.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host,ac-0c87gnx-shard-00-00.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host,ac-0c87gnx-shard-00-01.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host, Timeout: 30s, Topology Description: <TopologyDescription id: 62b882a72b6cb4b9ce4e8082, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('ac-0c87gnx-shard-00-00.jlotl6y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-0c87gnx-shard-00-00.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host')>, <ServerDescription ('ac-0c87gnx-shard-00-01.jlotl6y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-0c87gnx-shard-00-01.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host')>, <ServerDescription ('ac-0c87gnx-shard-00-02.jlotl6y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-0c87gnx-shard-00-02.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host')>]>
Python版本: 3.9.7
PyMongo 版本: 4.1.1
PyMongo 连接字符串:
mongodb+srv://ManbirJudge:[my-password]@cluster0.jlotl6y.mongodb.net/?retryWrites=true&w=majority
PyMongo 驱动程序代码:
import pymongo
client = pymongo.MongoClient("mongodb+srv://ManbirJudge:[my-password]@cluster0.jlotl6y.mongodb.net/?retryWrites=true&w=majority")
client.server_info() // for testing if connected
Pip 列表输出(我知道我已经安装了 Django,但这不是问题):
Package Version
------------------- -------
asgiref 3.5.2
cffi 1.15.0
cryptography 37.0.2
dj-database-url 0.5.0
Django 4.0.5
django-cors-headers 3.13.0
django-heroku 0.3.1
django-rest-knox 4.2.0
djangorestframework 3.13.1
dnspython 2.2.1
Pillow 9.1.1
pip 22.0.4
psycopg2 2.9.3
pycparser 2.21
pymongo 4.1.1
pytz 2022.1
setuptools 62.2.0
sqlparse 0.4.2
tzdata 2022.1
wheel 0.37.1
whitenoise 6.2.0
完整的堆栈跟踪:
Traceback (most recent call last):
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\test.py", line 4, in <module>
"mongodb+srv://ManbirJudge:rNn2qN8GBroMd2OJ@cluster0.jlotl6y.mongodb.net/?retryWrites=true&w=majority")
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\venv\lib\site-packages\pymongo\mongo_client.py", line 1724, in server_info
self.admin.command(
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\venv\lib\site-packages\pymongo\database.py", line 721, in command
with self.__client._socket_for_reads(read_preference, session) as (
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\venv\lib\site-packages\pymongo\mongo_client.py", line 1235, in _socket_for_reads
server = self._select_server(read_preference, session)
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\venv\lib\site-packages\pymongo\mongo_client.py", line 1196, in _select_server
server = topology.select_server(server_selector)
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\venv\lib\site-packages\pymongo\topology.py", line 251, in select_server
servers = self.select_servers(selector, server_selection_timeout, address)
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\venv\lib\site-packages\pymongo\topology.py", line 212, in select_servers
server_descriptions = self._select_servers_loop(selector, server_timeout, address)
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\venv\lib\site-packages\pymongo\topology.py", line 227, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: ac-0c87gnx-shard-00-00.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host,ac-0c87gnx-shard-00-01.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host,ac-0c87gnx-shard-00-02.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host, Timeout: 30s, Topology Description: <TopologyDescription id: 62b885958215d15ac9da754e, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('ac-0c87gnx-shard-00-00.jlotl6y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-0c87gnx-shard-00-00.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host')>, <ServerDescription ('ac-0c87gnx-shard-00-01.jlotl6y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-0c87gnx-shard-00-01.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host')>, <ServerDescription ('ac-0c87gnx-shard-00-02.jlotl6y.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('ac-0c87gnx-shard-00-02.jlotl6y.mongodb.net:27017: [WinError 10054] An existing connection was forcibly closed by the remote host')>]>
请不要不喜欢这个问题,我已经检查了 StackOverflow 上的其他解决方案,但没有适合我的解决方案。
或者我应该只使用 MongoDB 的本地服务器而不是 Atlas?
MongoDB Atlas “只允许客户端从项目的IP 访问列表中的条目连接到数据库部署”。
即使设置了初始 IP 地址以访问 Atlas,您也可能需要更改它和/或添加更多 IP 地址。 这可能是需要的,例如,如果您从不同的位置(家庭/工作/学校)通过不同的代理/VPN 进行连接,或者甚至您的 Internet 服务提供商 (ISP) 可能会更改您面向公众的 IP 地址(此一年发生在我身上好几次)。
Atlas 将查看它所看到的 IP 地址,并将其与项目的访问列表进行比较,并且仅在存在时才允许连接。 如果没有,您的连接可能会被拒绝和/或超时。
Atlas 允许在访问列表中设置IP 地址、CIDR 块或安全组 ID(仅限 AWS) ,以便您可以为您的应用程序定制访问权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.