ServerSelectionTimeoutError Error with FastAPI and MongoDB

I’ve been trying to connect my fast API app to the MongoDB atlas but I keep getting this error:

pymongo.errors.ServerSelectionTimeoutError: ac-0bb81fi-shard-00-01.t4cncq2.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:

By the way, I’m using macOS and I have seen similar questions/solutions but they all seem to be a halfway solution. I followed a solution from this post and went ahead to download a certificate from lets-encrypt but nothing happened.

I have these libraries installed:

dnspython ==2.4.2
pymongo==4.5.0
certifi==2023.7.22
beanie==1.22.5
motor ==3.3.1

MacOS and xcode are all updated.

Hey @Albert_Frimpong,

Welcome to the MongoDB Community!

I would recommend taking a look at our Which certificate authority signs MongoDB Atlas cluster TLS certificates? documentation.

Also, refer to the Troubleshooting TLS Errors documentation and Common SSL Issues on Python and How to Fix it.

In case the issue persists, please feel free to reach out.

Best regards,
Kushagra