Home > Net >  pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas
pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas

Time:06-27

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?

CodePudding user response:

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).

  • Related