无法连接到replicaset .net驱动程序 - MongoDB

时间:2014-09-10 09:02:06

标签: .net vb.net mongodb mongodb-.net-driver replicaset

我正在使用此代码:

Dim client = New MongoClient(ConnectionStringText)
mongo = client.GetServer()

在vb.net中,我使用.net的1.7 mongo驱动程序并更新到最新版本以确保它不是版本问题。

当我使用副本集连接字符串时:

mongodb://[myserver1]:27017,[myserver2]:27017,[myserver3]:27017/?replicaSet=myRepName

我收到以下错误:

Unable to connect to a member of the replica set matching the read preference Primary: The requested name is valid, but no data of the requested type was found

如果你像这样连接(只到一台服务器):

mongodb://[myserver1]:27017

代码效果很好

另外 - 我使用python来验证我的副本集是否使用了相同的连接字符串,这些字符串在这里工作并且运行良好

我也试过这些选项:

/?connect=replicaset&replicaset=myRsName&readPreference=SecondaryPreferred

我也尝试使用此代码执行此操作:

    Dim servers As List(Of MongoServerAddress) = New List(Of MongoServerAddress)()
    servers.Add(New MongoServerAddress(myserver1IP, 27017))
    servers.Add(New MongoServerAddress(myserver2IP, 27017))
    servers.Add(New MongoServerAddress("myserver3IP, 27017))
    Dim settingsNew = New MongoClientSettings()
    settingsNew.ConnectionMode = ConnectionMode.ReplicaSet
    settingsNew.ReplicaSetName = "myRepName"
    settingsNew.ReadPreference = New ReadPreference(ReadPreferenceMode.PrimaryPreferred)
    settingsNew.Servers = servers.ToArray()

    Dim client = New MongoClient(settingsNew)

得到了这个错误:

Unable to connect to a member of the replica set matching the read preference Primary

当登录到服务器(ssh)并使用rs.status()命令时,我得到了预期的结果,一切似乎都运行良好(正如我提到的python驱动程序一样有用......)

有什么想法吗?

编辑:

我认为这可能是同一个问题:

  

https://jira.mongodb.org/browse/CSHARP-826

我也尝试降级到1.8.3驱动程序而没有成功。

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题:

服务器在windows azure上运行(ubuntu 14) rs.status()中定义的IP地址是内部IP,

由于某些原因导致.net驱动程序无法正常工作(无法理解python如何运行良好)但是当将IP地址更改为公共的IP地址时,

我这样改了:

$conf = rs.conf()
$conf.members[0].host = "my_public_address:27017"
rs.reconfig($conf)

Used this article as a guideline - MONGODB: TROUBLESHOOTING “UNABLE TO CONNECT TO A MEMBER OF THE REPLICA SET MATCHING THE READ PREFERENCE PRIMARY”