MongoDB connection issue when deployed as app

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

1 Like

if you add 0.0.0.0 in mongo db it accepts requests from any IP

and it says handshake failed so its either
1)IP issue
2)taking too much time to load string from.env [faced by me in the paste] [but u r giving as string so there might not be any issue reg that

hope this will help

or if you want streamlit ip try catching it by making a request for the first time with wildcard and then use that ip ,ig this will work

3 Likes

Hey!
Thanks so much, adding the streamlit cloud ip to whitelist solved the issue!

For future reference! I added this bit of code to extract the streamlit ip per your suggestion and added it to my Network Access list. And it solved the timeout issue!
Thanks again :slight_smile:
Although, out of frustration i re-wrote everything with firebase back-end :joy: :joy:

import requests

def get_external_ip():
    response = requests.get("https://api64.ipify.org?format=json")
    if response.status_code == 200:
        data = response.json()
        return data.get("ip")
    else:
        return "Unknown"

external_ip = get_external_ip()
st.write("External IP:", external_ip)
2 Likes

Welcome!
All the best

LOL

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.