question

EthanSooHon-8648 avatar image
0 Votes"
EthanSooHon-8648 asked MikeUrnun edited

Cosmos DB Mongo Error: MongoServerSelectionError Server selection timed out after 30000ms Error

We are currently running multiple environments for our Cosmos DB using MongoDB APIs. In our dev environment we were able to successfully connect to Cosmos through the MongoDB package (mongodb) and successfully perform operations to create collections, add documents etc... but when the exact codebase was attempted to be used in the production environment the following error would propagate after trying to establish the initial connection to Cosmos (sometimes we could establish the initial connection to Cosmos but after a short period of time it would propagate the same error again)



Result: Failure Exception: MongoServerSelectionError: Server selection timed out after 30000 ms Stack: MongoServerSelectionError: Server selection timed out after 30000 ms at Timeout._onTimeout

(*Note: I have never seen this particular error when running Cosmos in our development environment)


To add additional information, we have an Azure functions app (running Node js) running that connects to Cosmos DB (via Mongodb) and performs operations.
We use the "primary connection" string for our Cosmos DB and insert it as appSettings for our Azure functions API.

Both the development and the productions servers are set up in the exact same manner they both have the same settings...

  • Access to all networks (so we know its not a IP whitelist error)

  • SSL is true (option is set in connection string)

  • Same Database/collection names

  • Same maxIdleRetry time setting (120000)

(here are the full connections string settings set ?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000)

Does anyone have any insight into this error? Am I missing a setting/option that needs to be set on the MongoDB connection?















azure-functionsazure-cosmos-db
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.

1 Answer

03709442 avatar image
2 Votes"
03709442 answered MikeUrnun edited

Hey there!

I'm actually facing the exact same problem here. One of the recommendations I saw was to not include the username/password in the connection string but rather pass it in the options like this:

 var DATABASE_URL = `mongodb://${process.env.MONGO_ENDPOINT}:${process.env.MONGO_PORT}/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@eccosmosdb@&directConnection=true`;
    
 const option = {
   auth: {
     user: process.env.MONGO_USERNAME,
     password: process.env.MONGO_PASSWORD,
  }
 };
    
 mongoose.connect(DATABASE_URL, option);

Have you tried this yet?

Bram

· 3
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 Bram,

Thanks for the quick reply! I will try this later to see if it improves the situation at all. I noticed in both the primary and secondary connection strings listed in the "Connection String" section of Cosmos it does not include the directConnection=true setting by default, was your problem resolved by this setting as well?

0 Votes 0 ·
MikeUrnun avatar image MikeUrnun EthanSooHon-8648 ·

Hey @EthanSooHon-8648 - Were you able to resolve this issue?

0 Votes 0 ·

Hi @MikeUrnun thank you for reaching out!

I tried the above suggestion and I was still sporadically running into the timeout / Mongodb ServerSelection error. It would connect and work for a few minutes then run into this issue again.

One other thing I have tried recently (that may have fixed it) is to upgrade the service tier (and # workers among other things) of the Azure functions Node API connecting to Cosmos; after doing this around 1-2 days ago it has not encountered this error so far so I am assuming this fixed but I'll check back here in a few days with the final results.

If there's anything else you think I can do to resolve this please let me know, thank you!202452-2022-05-16-16-15-56-window.png


0 Votes 0 ·