A timeout occured after 30000ms selecting a server using CompositeServerSelector

56,918

Solution 1

I am replacing the connection string method in like below.

new MongoClient("mongodb://username:[email protected]:11111/db-name")

Now it's solved.

Please see the answer from Paul Lemke.

Solution 2

Add "?connect=replicaSet" to the end of your connection string if connecting to MongoLab.

new MongoClient("mongodb://username:[email protected]:11111/db-name?connect=replicaSet")

This JIRA ticket has some details: https://jira.mongodb.org/browse/CSHARP-1160

Basically the default is to connect to a replica set member. But MongoLab's Single-Node settings are actually a single node replica set and this causes us to not trust it. Appending ?connect=replicaSet to your connection string will force the driver to move into replica set mode and all will work.

Found that info here.

Solution 3

Make sure your current ip address is white-listed in mongo db server. If you change your internet provider new IP needs to be white-listed.

Solution 4

Make Sure your auth db is set correctly.

I ran into this issue when I mentioned only the DB i wanted to connect to , and my auth db was different (other than admin db ).

The db-name in this line is considered as the auth DB .

new MongoClient("mongodb://username:[email protected]:11111/db-name?connect=replicaSet")

Then you can change the selected DB Later

mDb = mClient.GetDatabase(mongoDBName);

Solution 5

Same Error Message but not encountered with a MongoLabs deployment.

I just encountered the same error listed in the title with an Asp.Net Core App. My issue was due to an IOC configuration issue.

In my IOC container, my wrapped MongoClient instance was configured with a dependency transient lifestyle.

Per The MongoDb C# Driver:

It is recommended to store a MongoClient instance in a global place, either as a static variable or in an IoC container with a singleton lifetime.

I promoted the lifestyle of my object to a singleton and it resolved the issue.

I am using:

  • .Net Core 2.0
  • Mongo C# Driver version 2.5
  • Castle Windsor for my IOC version 3.3.0

Please reference the C# Driver Client section: http://mongodb.github.io/mongo-csharp-driver/2.5/reference/driver/connecting/#re-use

Share:
56,918
Ragesh S
Author by

Ragesh S

Welcome, my name is Ragesh P R. I am a software developer from Cochin India who loves to write software to build great products and help businesses succeed with their goals. I really encourage good design and I am seeing its importance more than ever in today's apps, websites, and products. I have been working professionally in software development since 2010 in the web and mobile spaces with experience across various domains. Skype : ragesh.sl

Updated on April 30, 2021

Comments

  • Ragesh S
    Ragesh S about 3 years

    I try to deploy my Mongo database in Mongolabs, everything works fine, and I create a new database. Please see my connectionstring.

        public DbHelper()
        {
    
            MongoClientSettings settings = new MongoClientSettings()
            {
                Credentials = new MongoCredential[] { MongoCredential.CreateCredential("dbname", "username", "password") },
                Server = new MongoServerAddress("ds011111.mongolab.com", 11111),
                //ConnectTimeout = new TimeSpan(30000)
            };
    
            Server = new MongoClient(settings).GetServer();
    
            DataBase = Server.GetDatabase(DatabaseName);
    
        }
    

    but when I try to connect the database it's shows error like:

    enter image description here

  • MordechayS
    MordechayS over 7 years
    Yep, MongoLab doesn't like new MongoSettings(new MongoUri("..."))
  • Komal Bansal
    Komal Bansal almost 4 years
    @RamyDeeb Its not just for the user who posted it , but also for those who are facing similar issues and viewing relevant answers
  • Gabriel Andrés Brancolini
    Gabriel Andrés Brancolini about 3 years
    @RamyDeeb the answer provided its also for others who has the same issue and can look for other possible solutions. Please, if you ever have another "solution", just add it, this platform is what it was built for!
  • Rodrigo Reis
    Rodrigo Reis almost 3 years
    Why didn't you mark his answer as the solution?