pymongo.Errors.ServerselectionTimeOuterror:repliceSetNoprimary -MongoDB Atlas
问题是,每当(我尝试过3次)时,我都会在MongoDB地图集上创建一个新的集群,并使用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 connection字符串:
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 LIST LIST OUTPUT安装了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:[email protected]/?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?
The problem is that whenever (I have tried 3 times) I create a new cluster on MongoDB Atlas and connect to it in Python using PyMongo, for the first 3-4 it connect painlessly but after sometime or after taking a break when I re-connect it spits out the following error:
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 version: 3.9.7
PyMongo version: 4.1.1
PyMongo connection string:
mongodb+srv://ManbirJudge:[my-password]@cluster0.jlotl6y.mongodb.net/?retryWrites=true&w=majority
PyMongo driver code:
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 list output (I know I have installted Django but that's not the problem):
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
Full stack trace:
Traceback (most recent call last):
File "D:\Manbir\Programming\Big Projects\(4) Chat App\server\test.py", line 4, in <module>
"mongodb+srv://ManbirJudge:[email protected]/?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')>]>
Please do not dislike the question, I have checked other solutions on StackOverflow but there was no solution that worked for me.
Or should I just use the local server of MongoDB instead of Atlas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mongodb atlas “仅允许客户连接来自项目的条目中的数据库部署“ https://www.mongodb.com/docs/atlas/security/ip-access-list/” rel =“ nofollow noreferrer”> ip访问列表“。
即使将初始的IP地址设置为访问地图集,您也可能需要更改和/或添加更多IP地址。例如,如果您从其他位置(家庭/工作/学校)连接到其他代理/VPN,甚至您的Internet服务提供商(ISP)可能会更改面向公共的IP地址(这是这样)每年发生几次)。
Atlas将查看其视为您的IP地址的内容,并将其与项目的访问列表进行比较,并且仅在连接时才允许。如果没有,您的连接可能会被拒绝和/或只是超时。
Atlas允许设置 IP地址,CIDR块或安全组ID(仅AWS)在访问列表中,因此您可以为应用程序量身定制访问权限。
MongoDB Atlas "only allows client connections to the database deployment from entries in the project's IP access list".
Even after setting an initial IP address to access Atlas, you may need to change it and/or add more IP addresses. This may be needed, for example, if you connect from a different location (home/work/school), through a different proxy/VPN, or maybe even your Internet Service Provider (ISP) might change your public-facing IP address (this happens to me several times a year).
Atlas will look at what it sees as your IP address and compare it to the project's access list and only allow connections if it is there. If not, your connection may be refused and/or just time out.
Atlas allows setting IP addresses, CIDR blocks, or Security Group IDs (AWS only) in the access list so you can tailor access for your application(s).