Pymongo getting timeout from atlas

Dear all,

this has been happening for a long time. I remember looking into different forums and lots of people saying they were having the same problem.

I have a simple script that connects to Mongo, but it is working intermittently. Most of the time doesn’t work and rarely works.

Here is the code:

from pymongo import MongoClient
myclient = MongoClient("mongodb+srv://LALALA:LALALA@makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority")
mydb = myclient["somedb"]
coll = mydb['mycoll']

print(coll.find())

And here is the error message:

pymongo.errors.ServerSelectionTimeoutError: No replica set members match selector “Primary()”, Timeout: 30s, Topology Description: <TopologyDescription id: 64426016e16bb77f0fd1ec49, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription (‘makesmiledb-shard-00-00.trs4s.mongodb.net’, 27017) server_type: RSSecondary, rtt: 0.02426325880332851>, <ServerDescription (‘makesmiledb-shard-00-01.trs4s.mongodb.net’, 27017) server_type: Unknown, rtt: None, error=NetworkTimeout(‘makesmiledb-shard-00-01.trs4s.mongodb.net:27017: timed out’)>, <ServerDescription (‘makesmiledb-shard-00-02.trs4s.mongodb.net’, 27017) server_type: RSSecondary, rtt: 0.02303117101703737>]>

Don’t know what else todo. As I said, sometimes it retrieve the data, and sometimes I get timeout.

Thanks.

Hi @Caio_Marcio, two questions,

  1. Can you please post the output of pip list?
  2. Could you try connecting with the mongodb shell?

I was able to connect to locally your cluster with PyMongo 4.3.3:

>>> from pymongo import MongoClient
>>> myclient = MongoClient("mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority")
>>> myclient.somedb.command('ping')
{'ok': 1}

Hi @Steve_Silvester , thanks for your answer.

There you go.

PING

Python 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import MongoClient
>>> myclient = MongoClient("mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority")
>>> myclient.somedb.command('ping')
Traceback (most recent call last):
  File "/usr/local/lib64/python3.11/site-packages/pymongo/pool.py", line 1358, in connect
    sock = _configured_socket(self.address, self.opts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pymongo/pool.py", line 1052, in _configured_socket
    sock = _create_connection(address, options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pymongo/pool.py", line 1036, in _create_connection
    raise err
  File "/usr/local/lib64/python3.11/site-packages/pymongo/pool.py", line 1029, in _create_connection
    sock.connect(sa)
TimeoutError: timed out
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib64/python3.11/site-packages/pymongo/_csot.py", line 105, in csot_wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pymongo/database.py", line 805, in command
    with self.__client._socket_for_reads(read_preference, session) as (
  File "/usr/lib64/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pymongo/mongo_client.py", line 1282, in _socket_from_server
    with self._get_socket(server, session) as sock_info:
  File "/usr/lib64/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pymongo/mongo_client.py", line 1217, in _get_socket
    with server.get_socket(handler=err_handler) as sock_info:
  File "/usr/lib64/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pymongo/pool.py", line 1407, in get_socket
    sock_info = self._get_socket(handler=handler)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pymongo/pool.py", line 1520, in _get_socket
    sock_info = self.connect(handler=handler)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib64/python3.11/site-packages/pymongo/pool.py", line 1366, in connect
    _raise_connection_failure(self.address, error)
  File "/usr/local/lib64/python3.11/site-packages/pymongo/pool.py", line 254, in _raise_connection_failure
    raise NetworkTimeout(msg) from error
pymongo.errors.NetworkTimeout: makesmiledb-shard-00-01.trs4s.mongodb.net:27017: timed out

PIP

[root@LAB ~]# pip3.11 list
Package Version
argcomplete 2.0.0
cffi 1.15.1
charset-normalizer 2.1.0
click 8.1.3
dasbus 1.6
dbus-python 1.3.2
distro 1.7.0
dnspython 2.3.0
file-magic 0.4.0
Flask 2.2.3
Flask-Cors 3.0.10
gpg 1.17.0
idna 3.3
itsdangerous 2.1.2
Jinja2 3.1.2
libcomps 0.1.18
MarkupSafe 2.1.2
nftables 0.1
pexpect 4.8.0
pip 22.2.2
ply 3.11
ptyprocess 0.6.0
pycparser 2.20
PyGObject 3.42.2
pymongo 4.3.3
PySocks 1.7.1
python-augeas 1.1.0
python-dateutil 2.8.2
requests 2.28.1
rpm 4.18.0
selinux 3.4
sepolicy 3.4
setools 4.4.0
setroubleshoot 3.3.31
setuptools 62.6.0
six 1.16.0
sos 4.4
systemd-python 235
urllib3 1.26.12
Werkzeug 2.2.3

MONGOSH

[root@LAB ~]# mongosh  'mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority'
Current Mongosh Log ID: 644302559e6d3fd5579fe4ec
Connecting to:          mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority&appName=mongosh+1.8.0
MongoServerSelectionError: Server selection timed out after 30000 ms
[root@LAB ~]# mongosh  'mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority'
Current Mongosh Log ID: 64430277f3f213c02e7fe426
Connecting to:          mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority&appName=mongosh+1.8.0
Using MongoDB:          6.0.5
Using Mongosh:          1.8.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-srdjma-shard-0 [primary] test> exit
[root@LAB ~]# mongosh  'mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority'
Current Mongosh Log ID: 64430284fd8412c7e567b211
Connecting to:          mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority&appName=mongosh+1.8.0
Using MongoDB:          6.0.5
Using Mongosh:          1.8.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-srdjma-shard-0 [primary] test> exit
[root@LAB ~]# mongosh  'mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority'
Current Mongosh Log ID: 64430288d6f8de4610cb6a4c
Connecting to:          mongodb+srv://makesmiledb.trs4s.mongodb.net/?retryWrites=true&w=majority&appName=mongosh+1.8.0
MongoServerSelectionError: Server selection timed out after 30000 ms

Tried a few times… some worked and some not.

Thanks.

Typically this error occurs when your client cannot connect to your cluster (ex: the application’s IP address is not in Atlas’ IP Access List ), or if there is a network issue between the client and the server.