問題是,每當(我已經嘗試過 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')>]>
蟒蛇版本: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:[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?
uj5u.com熱心網友回復:
MongoDB Atlas “只允許客戶端從專案的IP 訪問串列中的條目連接到資料庫部署”。
即使設定了初始 IP 地址以訪問 Atlas,您也可能需要更改它和/或添加更多 IP 地址。這可能是需要的,例如,如果您從不同的位置(家庭/作業/學校)通過不同的代理/VPN 進行連接,或者甚至您的 Internet 服務提供商 (ISP) 可能會更改您面向公眾的 IP 地址(此一年發生在我身上好幾次)。
Atlas 將查看它所看到的 IP 地址,并將其與專案的訪問串列進行比較,并且僅在存在時才允許連接。如果沒有,您的連接可能會被拒絕和/或超時。
Atlas 允許在訪問串列中設定IP 地址、CIDR 塊或安全組 ID(僅限 AWS),以便您可以為您的應用程式定制訪問權限。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/495726.html
上一篇:MongoServerError:cannotcreateanewcollectionalreadyusing500collectionof500,errorcode:8000
下一篇:MongoDB-更新所有地圖值
