Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MongoDb Connect to Replica Set Issue

According to the mongodb website, I should be able to connect to a replica set if I just give it one member from the replica set:

"The C# Driver is able to connect to a replica set even if the seed list is incomplete. It will find the primary server even if it is not in the seed list as long as at least one of the servers in the seed list responds (the response will contain the full replica set and the name of the current primary)." http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Connectionstrings

However, I cannot get my driver to connect if I just give it a secondary member.

This is my current connection statement:

m_server = MongoServer.Create(new MongoServerSettings { ConnectionMode = ConnectionMode.ReplicaSet, Server = new MongoServerAddress(connection) });

The 'connection' variable is: mongodb://servername/?safe=true

I saw this: https://jira.mongodb.org/browse/CSHARP-500, and I did run rs.status(), and did use the correct server name. Any help is appreciated!

like image 697
anthv123 Avatar asked Aug 08 '12 23:08

anthv123


2 Answers

At this moment I’m learning MongoDB and I’ve being playing around replica set connections. I like to contribute with 2 ways that I have used to connect to the database that I found useful, if doesn’t help anyone, at least I will have a place to refer to in the future (I’m sure I’m going to need it at some point) first:

var connString = "mongodb://localhost:27029,localhost:27027,localhost:27028?connect=replicaSet";
var client = new MongoClient(connString);
var db = client.GetDatabase("test");

second:

var settings = new MongoClientSettings
{
   Servers = new[]
   {
      new MongoServerAddress("localhost", 27027),
      new MongoServerAddress("localhost", 27028),
      new MongoServerAddress("localhost", 27029)
   },
   ConnectionMode = ConnectionMode.Automatic,
   ReplicaSetName = "m101",
   WriteConcern = new WriteConcern(WriteConcern.WValue.Parse("3"),wTimeout:TimeSpan.Parse("10"))
};
var client = new MongoClient(settings);

The first, allows me to connect to the database through the servers specified in the list of server. This allows the driver to connect automatically to the new principal node in the replica set in the case of failure with the principal. With the second, I send the list of servers in the replica set, the connection type. The name of the replica set, and the write concern configuration. With this settings, I’m forcing the driver to wait for an acknowledge of writing from the 3 servers in the replica set (WValue:3) and to wait at the most 10 seconds for the confirmation of writing.

like image 101
Alex Alvarez Avatar answered Dec 01 '22 01:12

Alex Alvarez


So, the connection variable is a full connection string, not something to pass to MongoServerAddress. Also, you can specify the connection mode on the connection string as well. Try this:

connection = "mongodb://servername/?safe=true&connect=replicaset";
m_server = MongoServer.Create(connectionString);
like image 34
Craig Wilson Avatar answered Dec 01 '22 02:12

Craig Wilson