[CSHARP-1298] Mongo DB Connection Issue - " MongoDB.Driver.MongoConnectionException: Unable to connect in the specified timeframe of '00:00:30' " Created: 27/May/15  Updated: 16/Nov/21  Resolved: 17/Jun/15

Status: Closed
Project: C# Driver
Component/s: Connectivity
Affects Version/s: 1.10
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Ankur Jasoria Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: query, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Visual Studio on Windows7



 Description   

Can Any body help me how to make connection string for MongoDB if conditional are as follows-
Database Name - ABC
Server Name - DEF1,DEF2
UserName - ankur
Password - ankur123
I have Created at My End below
"mongodb://ankur:ankur123@DEF1:27017,DEF2:27017/ABC?maxpoolsize=250;authSource=$external;authMechanism=PLAIN;safe=true"
and Error Occur - Exception: MongoDB.Driver.MongoConnectionException: Unable to connect in the specified timeframe of '00:00:30'.
Thanks.



 Comments   
Comment by Raju [X] [ 01/Feb/17 ]

I am not able to connect to mongodb using connect-mdbc from powershell.
Connect-Mdbc : Unable to connect in the specified timeframe of '00:00:30'.

Comment by Craig Wilson [ 15/Nov/16 ]

It was related to not being specific enough in the connection string. For instance, if you are talking to a replica set, you should specify the the replica set name in the connection string.

Comment by Amit Kumar [ 15/Nov/16 ]

Getting the same error with mongo driver version: 1.10.0.62
MongoDB.Driver.MongoConnectionException: Unable to connect in the specified timeframe of '00:00:30'.
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.Discover(TimeSpan timeout)
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.EnsureInstanceManager(TimeSpan timeout)
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.ChooseServerInstance(ReadPreference readPreference)
at MongoDB.Driver.MongoServer.RequestStart(MongoDatabase initialDatabase, ReadPreference readPreference)
at MongoDB.Driver.MongoDatabase.RequestStart(ReadPreference readPreference)
at MongoDB.Driver.MongoDatabase.RequestStart()

The issue seems closed so what was the resolution ?

Comment by Mikhail Vaganov [X] [ 20/Oct/15 ]

Thank you, Craig! It works very well!

Comment by Craig Wilson [ 16/Oct/15 ]

Absolutely... Let me know if that fixes the issue.

Comment by Mikhail Vaganov [X] [ 16/Oct/15 ]

Thank you for your answer, Craig! I'll test it. We have a replica set in our system.
But may I use MongoClientSettings instead of connection string?

Like this:

var credential = MongoCredential.CreateMongoCRCredential(_dataBaseName, _login, _password);
var settings = new MongoClientSettings
{
Credentials = new[]

{credential}

,
Servers = PrepareServersForConnection(),
ReplicaSetName = "AlarusReplicaSet"
};
var mongoClient = new MongoClient(settings);

Comment by Craig Wilson [ 16/Oct/15 ]

Hi Mikhail,

Since the problem seems to be around the DiscoveringMongoServerProxy, you can completely avoid this by telling us in the connection string what type of mongo cluster you are connecting to. For instance, if you are connecting to a replica set, provide the replica set name in the connection string: replicaSet=mySetName. If you are connecting to a shard router, provide the option connect=ShardRouter in the connection string.

I'm not saying we don't have a bug here, but this should work around your problem for the time being.

Craig

Comment by Mikhail Vaganov [X] [ 16/Oct/15 ]

We have the same issue. The interesting thing is that sometimes we have this exception:

MongoDB.Driver.MongoConnectionException: Unable to connect to server 192.168.100.101:27017: No connection could be made because the target machine actively refused it 192.168.100.101:27017. ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 192.168.100.101:27017
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
at MongoDB.Driver.Internal.MongoConnection.Open()
at MongoDB.Driver.Internal.MongoConnection.GetNetworkStream()
at MongoDB.Driver.Internal.MongoConnection.SendMessage(BsonBuffer buffer, Int32 requestId)
at MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage message)
at MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection connection)
at MongoDB.Driver.MongoServerInstance.RunCommandAs[TCommandResult](MongoConnection connection, String databaseName, IMongoCommand command)
at MongoDB.Driver.MongoServerInstance.Ping(MongoConnection connection)
at MongoDB.Driver.MongoServerInstance.Connect()
at MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan timeout, ReadPreference readPreference)
— End of inner exception stack trace —
at MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan timeout, ReadPreference readPreference)
at MongoDB.Driver.Internal.DirectMongoServerProxy.ChooseServerInstance(ReadPreference readPreference)
at MongoDB.Driver.MongoServer.AcquireConnection(ReadPreference readPreference)
at MongoDB.Driver.MongoDatabase.GetCollectionNames()
at MongoDB.Driver.MongoDatabase.CollectionExists(String collectionName)

But sometimes this:

MongoDB.Driver.MongoConnectionException: Unable to connect in the specified timeframe of '00:00:30'.
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.Discover(TimeSpan timeout)
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.EnsureInstanceManager(TimeSpan timeout)
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.ChooseServerInstance(ReadPreference readPreference)
at MongoDB.Driver.MongoServer.AcquireConnection(ReadPreference readPreference)
at MongoDB.Driver.MongoDatabase.GetCollectionNames()
at MongoDB.Driver.MongoDatabase.CollectionExists(String collectionName)

We found this exceptions during testing the case, when MongoDB starts after our application. There are two possible ways further: if we have the first type exception, then after starting of Mongo our Application can successfully connect to MongoDB: if we face with the second type exception, then the the Application cannot connect to MongoDB even after Mongo started,

Comment by Rami stern [ 28/May/15 ]

The same issue happend to us as well using driver version 1.10.0.62.
Just to add, while suffering from this error, we were able to connect to Mongo just fine using: the mongo.exe and a test executable. restarting Mongo didn't solve the issue. only restarting our application (which we can't do...) seemed to solve the issue.

Comment by Craig Wilson [ 27/May/15 ]

Thanks Ankur.

I'm having a hard time reconciling your stack trace with the 2.0 driver. Would you mind taking a screenshot of the properties window of the MongoDB.Driver.dll?

1. Find the MongoDB.Driver.dll on your system.
2. Right-click it and choose Properties
3. Select the Details tab
4. Take a screen shot
5. upload the screenshot to this ticket

The product version and file version on that tab should include the version of the driver your using.

Thanks,
Craig

Comment by Ankur Jasoria [ 27/May/15 ]

Craig,

We are working on updated one.

Thanks
Ankur

Comment by Craig Wilson [ 27/May/15 ]

Ankur,

In the AffectsVersion property, you have used 2.0. This stack trace is from a previous version of the driver. Could you please confirm what version of the driver you are using?

Thanks,
Craig

Comment by Ankur Jasoria [ 27/May/15 ]

Hi Craig,

Thanks for your quick reply !!
I am sharing you stack track also

Exception: MongoDB.Driver.MongoConnectionException: Unable to connect in the specified timeframe of '00:00:30'.
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.Discover(TimeSpan timeout)
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.EnsureInstanceManager(TimeSpan timeout)
at MongoDB.Driver.Internal.DiscoveringMongoServerProxy.ChooseServerInstance(ReadPreference readPreference)
at MongoDB.Driver.MongoServer.AcquireConnection(ReadPreference readPreference)
at MongoDB.Driver.MongoCursor`1.MongoCursorConnectionProvider.AcquireConnection()
at MongoDB.Driver.Operations.QueryOperation`1.GetFirstBatch(IConnectionProvider connectionProvider)
at MongoDB.Driver.Operations.QueryOperation`1.Execute(IConnectionProvider connectionProvider)
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at MongoDB.Driver.MongoCollection.FindOneAs[TDocument](FindOneArgs args)
at MongoRepository.MongoRepository`1.GetById(String collection, String id, String debugmode_)
at Fareportal.Crm.Services.NoteService.DefaultNoteService.Save(DefaultNote note_, String debugmode_),Inner Exception:,AnonymousId rBILP1U3gSOpul7QDddmAgabcdeAB==",

I have also figure out on these issue and here authentication working fine. At my end I am working at server end to figure out the issue if any issue at server end ?

If issue at code part end or MongoDB Driver end please figure out.

Thanks.
Ankur

Comment by Craig Wilson [ 27/May/15 ]

Hi Ankur,

Sorry you are having some trouble. Your connection string looks fine, but obviously there is a problem connecting to your servers.

1. There should be a lot more information in that exception than you are providing. Could you confirm that and, if so, please provide the entire stack trace?

2. The PLAIN mechanism is for using LDAP. I assume this is all setup and what you are wanting. Chances are that there is an authentication problem, and having the more full stack trace from (1) should help us identify that.

Thanks,
Craig

Generated at Wed Feb 07 21:39:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.