Summary
When I deploy the app using Streamlit Cloud I get an error for MongoDB database connection.
Steps to reproduce
Code snippet:
import streamlit as st
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
uri = "mongodb+srv://<username>:<password>@cluster0.oamoj2r.mongodb.net/?retryWrites=true&w=majority"
client = MongoClient(uri, server_api=ServerApi('1'), tls=True)
try:
client.admin.command('ping')
st.write("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
st.write(e)
Here’s the error I get:
Note: I got rid of all the (.) from mongododb.net to remove link markup!
ServerSelectionTimeoutError: SSL handshake failed: ac-yzwq2qj-shard-00-01.oamoj2r.mongodb net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129),SSL handshake failed: ac-yzwq2qj-shard-00-00.oamoj2r.mongodb net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129),SSL handshake failed: ac-yzwq2qj-shard-00-02.oamoj2r.mongodb net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129), Timeout: 30s, Topology Description: <TopologyDescription id: 64e36a4f5e508272db4e2032, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription (‘ac-yzwq2qj-shard-00-00.oamoj2r mongodb net’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘SSL handshake failed: ac-yzwq2qj-shard-00-00.oamoj2 mongodb net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129)’)>, <ServerDescription (‘ac-yzwq2qj-shard-00-01.oamoj2r mongodb net’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘SSL handshake failed: ac-yzwq2qj-shard-00-01.oamoj2r.mongodb net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129)’)>, <ServerDescription (‘ac-yzwq2qj-shard-00-02.oamoj2r.mongodb net’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘SSL handshake failed: ac-yzwq2qj-shard-00-02.oamoj2r.mongodb net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1129)’)>]>
Expected behavior:
It connects to the database when I run streamlit locally, it connects to the database. No issue.
Actual behavior:
Throws ServerSelectionTimeoutError. Not sure if I’ll need to add streamlitcloud ip as whitelist to mongodb atlas. Also not sure how to get the ip for streamlitcloud.
Any help would be appreciated.
Thanks
Debug info
streamlit=1.17.0
pymongo==4.4.1