I cant add a post to my db

Traceback (most recent call last):
  File "/Users/aarik/python/test.py", line 4, in <module>
    cluster = MongoClient("mongodb+srv://aarik:<i already put in a password here>@cluster0.uyuc29f.mongodb.net/?retryWrites=true&w=majority")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pymongo/mongo_client.py", line 677, in __init__
    res = uri_parser.parse_uri(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pymongo/uri_parser.py", line 455, in parse_uri
    raise ConfigurationError(
pymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs. To fix this error install pymongo with the srv extra:
 /usr/local/bin/python3.10 -m pip install "pymongo[srv]"
➜  python -m pip install "pymongo[srv]"
zsh: command not found: -m
➜  python pip install "pymongo[srv]"
Requirement already satisfied: pymongo[srv] in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (4.1.1)
Collecting dnspython<3.0.0,>=1.16.0
  Downloading dnspython-2.2.1-py3-none-any.whl (269 kB)
     |████████████████████████████████| 269 kB 2.1 MB/s            
Installing collected packages: dnspython
Successfully installed dnspython-2.2.1
➜  python /usr/local/bin/python3.10 /Users/aarik/python/test.py
Traceback (most recent call last):
  File "/Users/aarik/python/test.py", line 4, in <module>
    cluster = MongoClient("mongodb+srv://aarik:<already put in a password>@cluster0.uyuc29f.mongodb.net/?retryWrites=true&w=majority")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pymongo/mongo_client.py", line 677, in __init__
    res = uri_parser.parse_uri(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pymongo/uri_parser.py", line 455, in parse_uri
    raise ConfigurationError(
pymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs. To fix this error install pymongo with the srv extra:
 /usr/local/bin/python3.10 -m pip install "pymongo[srv]"
➜  python /usr/local/bin/python3.6 /Users/aarik/python/test.py
/usr/local/bin/python3.6 /Users/aarik/python/test.py
/usr/local/bin/python3.6 /Users/aarik/python/test.py
Traceback (most recent call last):
  File "/Users/aarik/python/test.py", line 10, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/collection.py", line 613, in insert_one
    comment=comment,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/collection.py", line 547, in _insert_one
    self.__database.client._retryable_write(acknowledged, _insert_command, session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1398, in _retryable_write
    with self._tmp_session(session) as s:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1676, in _tmp_session
    s = self._ensure_session(session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1663, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1608, in __start_session
    self._topology._check_implicit_session_support()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/topology.py", line 519, in _check_implicit_session_support
    self._check_session_support()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/topology.py", line 536, in _check_session_support
    readable_server_selector, self._settings.server_selection_timeout, None
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/topology.py", line 229, in _select_servers_loop
    % (self._error_message(selector), timeout, self.description)
pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: ac-oucby4l-shard-00-02.uyuc29f.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: ac-oucby4l-shard-00-01.uyuc29f.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852),SSL handshake failed: ac-oucby4l-shard-00-00.uyuc29f.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852), Timeout: 30s, Topology Description: <TopologyDescription id: 631e0275062c9e52d5465ad0, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('ac-oucby4l-shard-00-00.uyuc29f.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ac-oucby4l-shard-00-00.uyuc29f.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>, <ServerDescription ('ac-oucby4l-shard-00-01.uyuc29f.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ac-oucby4l-shard-00-01.uyuc29f.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>, <ServerDescription ('ac-oucby4l-shard-00-02.uyuc29f.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('SSL handshake failed: ac-oucby4l-shard-00-02.uyuc29f.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)',)>]>

how can i fix this

Hi @Aarik_Ghosh welcome to the community!

Are you still seeing this issue? If yes, could you please follow the troubleshooting steps described here Keep getting ServerSelectionTimeoutError and see if it fixes it?

If not, could you specify your OS version, and the code in the file test.py

Best regards
Kevin

I am using macOS Monterey Version 12.4. The code in test.py is:

import pymongo
from pymongo import MongoClient

cluster = MongoClient("mongodb+srv://aarik:<i entered the correct password here>@cluster0.uyuc29f.mongodb.net/?retryWrites=true&w=majority")
db = cluster["testDatabase"]
collection = db["testName"]

post = {"_id": 0, "name": "aarik", "score": 5}

collection.insert_one(post)

The test.py looks straightforward enough, thanks for confirming. The log messages you posted earlier seem to point to a connection issue, though. Could you confirm if the troubleshooting steps in the previously linked topic helped with the issue?

Best regards
Kevin

I read through it, but I’m not sure about what to change.

I think before that, you should try the suggestions in Troubleshoot Connection Issues.

Typical connection issues are:

  • IP whitelisting issues (e.g. Atlas was not setup to allow connection from the IP you’re connecting from)
  • Network policy issues (e.g. your PC not allowed to connect to the outside world in non-web ports by your network admin)
  • OS-level security issues (e.g. the OS disallowing connection due to some security policy)
  • TLS certificate issues (e.g. the certificate stored in the OS is outdated, or other similar issues)

Please note that these are just off the top of my head and is not an exhaustive list of possible causes.

Sometimes if it’s a company-issued laptop/PC, there are restrictions placed by the company’s IT department that just cannot be worked around without consulting with them (this is typically OS-level or network-policy level restrictions).

I think when faced with connection issues, the first step is to determine which of the four points above is the culprit.

Best regards
Kevin

1 Like

Just a question, does Atlas work on MacOS?

MongoDB Atlas is a managed service, so it works with any OS; as long as you have a way to communicate with the servers on port 27017.

Best regards
Kevin

1 Like

I used the option that let’s you connect to a database using Visual Studio Code extension, but when I do everything correctly and press Connect, I get

Unable to connect: connection <monitor> to 52.200.166.55:27017 closed

Now I was able to conncet using the Visual Studio Code option, but the original code still doesn’t work

Hi @Aarik_Ghosh

I’m not sure I understand. Can you connect to Atlas now, but the code doesn’t work? Is there any error you’re seeing?

Best regards
Kevin