我是否仍应在我的连接字符串中指定 Mongo 的副本集?
Should I still specify Mongo's replicaset in my connection string?
我正在使用 Mongo 和 C# mongo 驱动程序(2.10).
我已经设法通过副本集连接到它:
var dbClient = new MongoClient(
"mongodb://aaa:123@m.com:27017,m1.com:37017,m2.com:47017/dbtest?safe=true&connect=replicaset");
但是I read that I don't需要注明connect=replicaset
因为我有多个主机
这与建议 here.
相反
所以现在我的连接是:
"mongodb://aaa:123@m.com:27017,m1.com:37017,m2.com:47017/dbtest?safe=true"
— 有效。
但后来我读了 docs (and also here),没有 connect=replicaset
开关。
只有:replicaset=name
开关。
问题:
声明使用副本集的连接字符串(与 MongoClient C# 一起使用)的正确方法是什么?
随着时间的推移,MongoDB C# 驱动程序发生了很多变化。您的决定需要基于您使用的版本。
First link 2014年为perl驱动编写
SO link written in 2015 (when ver 2.2 was being used). There is no reference to connect=replicaset in any of the various versions documentation
Documentation 这是我推荐使用的,因为它是由 Mongo 编写的。按照这些文档中的说明,我已经能够使用 MongoClient 和 MongoClient 设置(如下示例)。
例子
你可以像上面那样把你的 mongodb 写成一个长字符串:
"mongodb://aaa:123@m.com:27017,m1.com:37017,m2.com:27017/dbtest?replicaSet=myRepl"
推荐
我建议使用本机 C# 变量连接到副本集,而不是像上面那样使用连接字符串。 ConnectionMode 在设置中指定是 ReplicaSet
还是 Direct
.
var mongoClientSettings = new MongoClientSettings()
{
ConnectionMode = ConnectionMode.ReplicaSet,
Credential = MongoCredential.CreateCredential("admin", "user", "pass"),
ReplicaSetName = "ReplicaSetName",
Servers = new List<MongoServerAddress>() { new MongoServerAddress("host", 27017), new MongoServerAddress("host2", 27017) }.ToArray(),
ApplicationName = "NameOfYourApplicatino",
};
MongoClient client = new MongoClient(mongoClientSettings);
由于客户端是线程安全的,您也可以将其用作全局变量。
我正在使用 Mongo 和 C# mongo 驱动程序(2.10).
我已经设法通过副本集连接到它:
var dbClient = new MongoClient(
"mongodb://aaa:123@m.com:27017,m1.com:37017,m2.com:47017/dbtest?safe=true&connect=replicaset");
但是I read that I don't需要注明connect=replicaset
因为我有多个主机
这与建议 here.
所以现在我的连接是:
"mongodb://aaa:123@m.com:27017,m1.com:37017,m2.com:47017/dbtest?safe=true"
— 有效。
但后来我读了 docs (and also here),没有 connect=replicaset
开关。
只有:replicaset=name
开关。
问题:
声明使用副本集的连接字符串(与 MongoClient C# 一起使用)的正确方法是什么?
随着时间的推移,MongoDB C# 驱动程序发生了很多变化。您的决定需要基于您使用的版本。
First link 2014年为perl驱动编写
SO link written in 2015 (when ver 2.2 was being used). There is no reference to connect=replicaset in any of the various versions documentation
Documentation 这是我推荐使用的,因为它是由 Mongo 编写的。按照这些文档中的说明,我已经能够使用 MongoClient 和 MongoClient 设置(如下示例)。
例子
你可以像上面那样把你的 mongodb 写成一个长字符串:
"mongodb://aaa:123@m.com:27017,m1.com:37017,m2.com:27017/dbtest?replicaSet=myRepl"
推荐
我建议使用本机 C# 变量连接到副本集,而不是像上面那样使用连接字符串。 ConnectionMode 在设置中指定是 ReplicaSet
还是 Direct
.
var mongoClientSettings = new MongoClientSettings()
{
ConnectionMode = ConnectionMode.ReplicaSet,
Credential = MongoCredential.CreateCredential("admin", "user", "pass"),
ReplicaSetName = "ReplicaSetName",
Servers = new List<MongoServerAddress>() { new MongoServerAddress("host", 27017), new MongoServerAddress("host2", 27017) }.ToArray(),
ApplicationName = "NameOfYourApplicatino",
};
MongoClient client = new MongoClient(mongoClientSettings);
由于客户端是线程安全的,您也可以将其用作全局变量。