C# MongoDb Connect to Replica Set Issue

淺唱寂寞╮ 提交于 2020-05-27 06:03:22

问题


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!


回答1:


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);



回答2:


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.



来源:https://stackoverflow.com/questions/11874796/c-sharp-mongodb-connect-to-replica-set-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!