Reputation: 19
I am trying to develop a RAG using AzureOpenAI and CosmosDB I have created a Azure Cosmos DB for MongoDB (vCore) in azure. I am trying to connect to the db but getting ServerSelectionTimeoutError.
Trying to Set up the connection through below python code:
from urllib.parse import quote_plus
username = quote_plus("username")
password = quote_plus("password")
server = "service_name.mongocluster.cosmos.azure.com"
mongo_conn = f"mongodb+srv://{username}:{password}@{server}/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"
mongo_client = pymongo.MongoClient(mongo_conn)
This code runs successfully with below warning:
C:\Users\AppData\Local\Temp\ipykernel_3292\24156620.py:9: UserWarning: You appear to be connected to a CosmosDB cluster. For
more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb mongo_client = pymongo.MongoClient(mongo_conn)
Then I try to execute the below code for Set up the DB and collection
# create a database called TryDB
db = mongo_client['TryDB']
# Create collection if it doesn't exist
COLLECTION_NAME = "Try_Collection"
collection = db[COLLECTION_NAME]
if COLLECTION_NAME not in db.list_collection_names():
# Creates a unsharded collection that uses the DBs shared throughput
db.create_collection(COLLECTION_NAME)
print("Created collection '{}'.\n".format(COLLECTION_NAME))
else:
print("Using collection: '{}'.\n".format(COLLECTION_NAME))
This code block gives me below error:
ServerSelectionTimeoutError: c.service_name.mongocluster.cosmos.azure.com:10260: timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 627, topology_type: Unknown, servers: [<ServerDescription ('c.service_name.mongocluster.cosmos.azure.com', 1020) server_type: Unknown, rtt: None, error=NetworkTimeout('c.service_name.mongocluster.cosmos.azure.com:1020: timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>
Upvotes: 1
Views: 366
Reputation: 11
I met a similar issue when I try to connect to Azure CosmosDB with MongoAPI (vCore) from my MacBook. The error is
pymongo.errors.ServerSelectionTimeoutError: c.<account name>.mongocluster.cosmos.azure.com:10260: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000) (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 66806284a3eab282240d69eb, topology_type: Unknown, servers: [<ServerDescription ('c.<account name>.mongocluster.cosmos.azure.com', 10260) server_type: Unknown, rtt: None, error=AutoReconnect('c.<account_name>.cosmos.azure.com:10260: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000) (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>
But there is no issue to connect on Windows. On MacBook, I double click 'Install Certificates.command' under 'Applications/Python 3.12/' folder to resolve this issue. My Python version is 3.12.3. Hope it helps.
Upvotes: 1