Streamlit Cloud error for MongoDB database connection

When I deploy the app using Streamlit Cloud I get an error for MongoDB database connection.

I setted the secret with the format

[mongo]
host = "xxx.mongodb.net"
port = 27017
username = "xxx"
password = "xxx"

And i’m trying to make the connection with:

client = pymongo.MongoClient(**st.secrets["mongo"])

I’ve added the IP 0.0.0.0/0 at the MongoDB network access but when i want to connect with the database i’m getting this error:

ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: neurochat.0snevsl.mongodb.net:27017: [Errno -5] No address associated with hostname (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 65df6d327d89f05c1d2bd82b, topology_type: Unknown, servers: [<ServerDescription ('neurochat.0snevsl.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('neurochat.0snevsl.mongodb.net:27017: [Errno -5] No address associated with hostname (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>

The error message is relatively clear, isn’t it? The hostname cannot be resolved, so it can be reached.

Sorry i’m relative new on adding a DB to my project. I copied the host from the uri provided by mongodb Atlas for python connection. Where do i get the correct one?

I cannot help with Atlas, never used it. I assume that you have to loosen access rights or whitelist your clients ip, because i cannot even ping this domain. Does the streamlit app has access to the atlas mongodb if you run it on your local computer?

1 Like

Solved. I don’t know which was the problem but i think is a problem with the port because when i replaced the [mongo] secret format with:

MONGODB_PASSWORD = "password"

and make the connection with:

client = pymongo.MongoClient(f"mongodb+srv://<username>:{st.secrets['MONGODB_PASSWORD']}@<host>/?retryWrites=true&w=majority&appName=<AppName>")

it worked

1 Like

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