Cosmos for MongoDB python sdk not able to connect

HTH-3183 1 Reputation point
2023-12-28T16:18:58.91+00:00

Hello, I'm following all the code examples for the azure-cosmos python sdk to connect to my cosmos for mongodb and I get errors. I have Public network access for all networks and I'm using azure-cosmos from pip as said in the samples for python documentation: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/samples-python

I got the uri and port from settings > connections strings and the key from the primary password (read-write)
I'm out of ideas, would appreciate help here.

HOST = "https://my-cosmos.mongo.cosmos.azure.com:10255/" 
MASTER_KEY = "my_primary_key=="
client = cosmos_client.CosmosClient(HOST, credential=MASTER_KEY )

>> Exception has occurred: ServiceResponseError ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
connection_string = "AccountEndpoint=https://my-cosmos.mongo.cosmos.azure.com:10255/;AccountKey=my_key"
client = cosmos_client.CosmosClient.from_connection_string(connection_string)
>> Exception has occurred: ServiceResponseError ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))


Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,906 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sajeetharan 2,261 Reputation points Microsoft Employee
    2023-12-28T19:09:07.8066667+00:00

    The sample that you're following is applicable to only NOSQL (Core) API of Azure Cosmos DB.

    To use Cosmos DB Mongo API with Python you can use any of the opensource drivers for python.

    Follow this sample here.


  2. Oury Ba-MSFT 20,926 Reputation points Microsoft Employee Moderator
    2023-12-28T20:13:29.68+00:00

    HTH-3183

    Thank you for reaching out.

    My understanding is that you are facing an issue while connecting your Azure cosmos db Mongo db Python SDK.

    The sample you linked is only for Azure Cosmos DB for NoSQL accounts and isn’t compatible with the API for MongoDB. All of the SDKs developed and owned by our team are only for the NoSQL API, and you can use the native SDKs themselves to connect to the OSS APIs (MongoDB, Cassandra etc).

     There’s a tutorial on how to connect to an Azure Cosmos DB for MongoDB account using python here: Get started with Azure Cosmos DB for MongoDB and Python | Microsoft Learn

    Regards,

    Oury


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.