简体   繁体   English

使用C#驱动程序的MongoDB Replicaset连接字符串

[英]MongoDB Replicaset Connection string using C# Driver

I have My Mongo DB set with a replica set of 3. One Primary and Two secondaries. 我的Mygo数据库设置为3个副本集。一个主要副本和两个次要副本。

 var connectionString = ConfigurationManager.AppSettings["MongoDBWriteCS"];
 var client = new MongoClient(connectionString);
 _MongoWriteServer = client.GetServer();
 _WriteDatabase = _MongoWriteServer.GetDatabase("DBName");
_WriteDatabase.GetCollection<CollectionType>("CollectionName").Insert(Object);

When my code runs with the below connection string, it has no issues to insert records 当我的代码使用以下连接字符串运行时,插入记录没有问题

<add key="MongoDBWriteCS" value="mongodb://username:password@10.0.0.0:27019/admin?w=0" />

But the problem is, since it is on replica set, my primary keeps changing when primary goes from 27019 to 27018 or 27017 inserts fails. 但是问题是,由于它位于副本集上,因此当主数据库从27019变为27018或27017插入失败时,我的主要数据库会不断更改。

So I tried to change my connection string as more authentic Replica set connection string. 因此,我尝试将连接字符串更改为更真实的副本集连接字符串。

<add key="MongoDBWriteCS" value="mongodb://username:password@10.0.0.0:27017,10.0.0.0:27018,10.0.0.0:27019/admin?replicaSet=myRepSet&amp;readPreference=primaryPreferred&amp;w=0" />

It keeps failing with " No such host " or " unable to connect to a member ",but with in the same line of code get list of collections works (I mean reads works only writes fails like insert or save commands) 它总是失败,并显示“ 没有这样的主机 ”或“ 无法连接到成员 ”,但是在同一行代码中,获取集合列表的工作正常(我的意思是,读取工作仅像插入或保存命令那样失败)

I am using MongoDB 2.6.4 我正在使用MongoDB 2.6.4

rs.status() rs.status()

/* 0 */
{
    "set" : "rbRepSet",
    "date" : ISODate("2015-03-09T23:27:17.000Z"),
    "myState" : 1,
    "members" : [ 
        {
            "_id" : 0,
            "name" : "haboMongo:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 59570,
            "optime" : Timestamp(1425941592, 5),
            "optimeDate" : ISODate("2015-03-09T22:53:12.000Z"),
            "lastHeartbeat" : ISODate("2015-03-09T23:27:16.000Z"),
            "lastHeartbeatRecv" : ISODate("2015-03-09T23:27:17.000Z"),
            "pingMs" : 0,
            "syncingTo" : "haboMongo:27019"
        }, 
        {
            "_id" : 1,
            "name" : "haboMongo:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 2220179,
            "optime" : Timestamp(1425941592, 5),
            "optimeDate" : ISODate("2015-03-09T22:53:12.000Z"),
            "lastHeartbeat" : ISODate("2015-03-09T23:27:17.000Z"),
            "lastHeartbeatRecv" : ISODate("2015-03-09T23:27:16.000Z"),
            "pingMs" : 0,
            "syncingTo" : "haboMongo:27019"
        }, 
        {
            "_id" : 2,
            "name" : "haboMongo:27019",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 2220202,
            "optime" : Timestamp(1425941592, 5),
            "optimeDate" : ISODate("2015-03-09T22:53:12.000Z"),
            "electionTime" : Timestamp(1425100988, 1),
            "electionDate" : ISODate("2015-02-28T05:23:08.000Z"),
            "self" : true
        }
    ],
    "ok" : 1
}

rs.config() rs.config()

/* 0 */
{
    "_id" : "rbRepSet",
    "version" : 3,
    "members" : [ 
        {
            "_id" : 0,
            "host" : "haboMongo:27017"
        }, 
        {
            "_id" : 1,
            "host" : "haboMongo:27018"
        }, 
        {
            "_id" : 2,
            "host" : "haboMongo:27019"
        }
    ]
}

Because you have used hostnames in your replica set configuration, the driver will discover those hostnames and use them instead of the ip addresses in your connection string. 因为您在副本集配置中使用了主机名,所以驱动程序将发现这些主机名并使用它们代替连接字符串中的IP地址。 Therefore, your hostnames MUST be resolvable by your client box. 因此,您的主机名必须在客户端框内可解析。 I'd recommend you use hostnames, but if for some reason you can't, then you'll need to put IP addresses into your replica set config. 我建议您使用主机名,但是如果出于某些原因不能使用主机名,则需要将IP地址放入副本集配置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM