question

DakaraiMcCoy-2805 avatar image
0 Votes"
DakaraiMcCoy-2805 asked DakaraiMcCoy-2805 answered

MongoDB API Cannot connect to CosmosDB using URI string: No replica set members found yet

Hello,

I am attempting to connect to the Azure CosmosDB using MongoDB API in python and am receiving a ServerSelectionTimeoutError. I am using the read-write key provided in the quickstart connection guide. The default timeout time is 30 seconds. Below is a test for connection with modification for a 1ms timeout error that also fails. I experimented with 0ms and even longer than 30 seconds with no success.

  `try: 
             client = pymongo.MongoClient(uri, serverSelectionTimeoutMS=1) 
             client.server_info() 
  except pymongo.errors.ServerSelectionTimeoutError as err: 
             print(err)` 

here is the error from the above code.

No replica set members found yet, Timeout: 0.001s, Topology Description: <TopologyDescription id: 6039226389effc87c5d93363, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('<server>.mongo.cosmos.azure.com', port) server_type: Unknown, rtt: None>]>


I even tried connecting using my local mongo shell but that isn't working either. I received a similar error with exit code 1. But checking the mongodb docs, there isn't an error code 1

C:\Program Files\MongoDB\Server\4.4\bin> mongo.exe <server>:port -u <uname> -p <password> == --tls --tlsAllowInvalidCertificates MongoDB shell version v4.4.3 connecting to: mongodb://<server>.mongo.cosmos.azure.com:port/test?compressors=disabled&gssapiServiceName=mongodb Error: couldn't connect to <server>.mongo.cosmos.azure.com, connection attempt failed: NetworkTimeout: Error connecting to <server>.mongo.cosmos.azure.com:port (IP) :: caused by :: Socket operation timed out : connect@src/mongo/shell/mongo.js:374:17 @(connect):2:6 exception: connect failed exiting with code 1

Do I need to configure the PyMongo API with some specific TLS settings? I checked the documentation and it suggests I might need to add a path to a *.pem file, however I didn't find any such suggestion in Azure ComosDB documentation (tutorials, how-to-guides, or quickstart).
Code and errors are modified for privacy.


azure-cosmos-db
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DakaraiMcCoy-2805

We are checking this and will get back to you.

Regards
Navtej S

0 Votes 0 ·
DakaraiMcCoy-2805 avatar image
0 Votes"
DakaraiMcCoy-2805 answered

Issue has been resolved. It was an internal Firewall issue.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AnuragSharma-MSFT avatar image
0 Votes"
AnuragSharma-MSFT answered

Hi @DakaraiMcCoy-2805, welcome to Microsoft Q&A forum.

I was trying to repro the error by connecting to Azure Cosmos DB Mongo DB API using python and was able to set similar error:

 pymongo.errors.ServerSelectionTimeoutError: xxxxxxxxxxxxxx.mongo.cosmos.azure.com:10255: timed out

Initially I though it could be a problem from the Azure Service itself but when I tried connecting from .NET code it worked fine. So did a little research and found an article which had similar issue:

Connecting to a Microsoft Azure Cosmos DB with Python and the MongoDB API

Made below changes in the connection string as per the above article and it worked fine:

 db_name = "test"
 host = "xxxxxxxx.mongo.cosmos.azure.com"
 port = 10255
 username = "xxxxxxxx"
 password = "xxxxxxxxx"
 args = "ssl=true&retrywrites=false&ssl_cert_reqs=CERT_NONE"
    
 connection_uri = f"mongodb://{username}:{password}@{host}:{port}/{db_name}?{args}"
    
 client = MongoClient(connection_uri)
    
 db = client[db_name]
 user_collection = db['user']
    
 # Save to the DB
 user_collection.insert_one({"email": "test2@foobar.com"})
    
 # Query the DB
 for user in user_collection.find():
     print(user)

Can you once try changing your code according to this and let us know if it works?


If answer helps, please mark it 'Accept Answer'



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DakaraiMcCoy-2805 avatar image
0 Votes"
DakaraiMcCoy-2805 answered AnuragSharma-MSFT commented

HI @AnuragSharma-MSFT and @NevtejSaini-MSFT

thank you for writing back.
I tried the above with the arguments you suggested and still I receive this error. I should also let you know I've tried using the primary and secondary connection strings provided in the connection string associated with the server.


connection_uri = f"mongodb://{username}:{password}@{host}:{port}/{db_name}?{args}"
client = pymongo.MongoClient(connection_uri)
client.server_info()

Traceback (most recent call last) File "C:\Users\{user}\.conda\envs\ExcelPythonEnv\lib\site-packages\pymongo\topology.py", line 215, in _select_servers_loop raise ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError:<host>:10255: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 603e519daec6d693ca000877, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription (<host>, 10255) server_type: Unknown, rtt: None, error=NetworkTimeout('<host>:10255: timed out')>]>

try: client = pymongo.MongoClient(uri, serverSelectionTimeoutMS=1) client.server_info() except pymongo.errors.ServerSelectionTimeoutError as err: print(err)'

No replica set members found yet, Timeout: 0.001s, Topology Description: <TopologyDescription id: 603e454daec6d693ca000876, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription (<host>, 10255) server_type: Unknown, rtt: None>]>


since I received another network error I don't think SSL/TLS is even able to attempt a secure connection. Seems I cannot find the host at all because rtt = None. So I checked the Microsoft management console and I don't see any certificates or keys. Not sure if I need to generate a new key pair.
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DakaraiMcCoy-2805, thanks for replying back. I am looking into it and shall get back at the earliest.

1 Vote 1 ·

Hi @DakaraiMcCoy-2805, thanks for your patience.

I tried replicating it but was able to connect properly with the same code. While searching the error message you provided, below is the code piece from the article which worked for similar error. Could you please try and check if it works?

 connectTimeoutMS=30000, socketTimeoutMS=None, socketKeepAlive=True, connect=False, maxPoolsize=1


0 Votes 0 ·

HI Anurag,

I updated my connection string arragementThis also didn't work. However updating my connection string options I receive a new error.
line 215, in _select_servers_loop raise ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError: <hosst>connection failed because connected host has failed to respond, Timeout: 30s, Topology Description: <TopologyDescription id: 6047a45e4213df8b2cfb1cb5, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('<host>', 10255) server_type: Unknown, rtt: None, error=AutoReconnect('<host>:<port>: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond')>]>


0 Votes 0 ·
Show more comments