0

I have My Mongo DB set with a replica set of 3. One Primary and Two secondaries.

 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.

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

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

/* 0 */
{
    "_id" : "rbRepSet",
    "version" : 3,
    "members" : [ 
        {
            "_id" : 0,
            "host" : "haboMongo:27017"
        }, 
        {
            "_id" : 1,
            "host" : "haboMongo:27018"
        }, 
        {
            "_id" : 2,
            "host" : "haboMongo:27019"
        }
    ]
}
HaBo
  • 13,999
  • 36
  • 114
  • 206
  • I'm not sure what the problem from your brief description " my primary keeps changing when primary goes from 27019 to 27018 or 27017 inserts fails", but, unless itt's a copy/paste problem, you have &'s in your connection uri instead of & – wdberkeley Mar 09 '15 at 17:34
  • We need to see your replica set config. Could you post the results of this from the shell? rs.config(); – Craig Wilson Mar 09 '15 at 18:25
  • @CraigWilson added the config info – HaBo Mar 09 '15 at 23:29
  • So, even though you are connecting with IP addresses, we actually will connect with the hostnames as configured. Can you verify that you can resolve each of those hostnames from your client machine? – Craig Wilson Mar 09 '15 at 23:46
  • Yes. I confirm. Because it works when I give my connection string as and other two ports as well – HaBo Mar 10 '15 at 02:54
  • I don't think I was clear. Can you connect when you use this app setting, where the connection string includes the dns name and not the IP address. – Craig Wilson Mar 10 '15 at 14:29
  • @CraigWilson no not with haboMongo:27019, since my IIS and Mongo are on two different servers. So I use IP and resolve the IP (10.0.0.0:27019) to haboMongo:27019 – HaBo Mar 10 '15 at 15:03

1 Answers1

1

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. 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.

Craig Wilson
  • 12,174
  • 3
  • 41
  • 45
  • I'd be surprised that it actually does on a long-running basis. You might see a bit of success right at the beginning of your program (where the insert is), and then it will start to fail shortly after. – Craig Wilson Mar 10 '15 at 15:37
  • IPs keep changing right. isn't this the right way to keep it like a name and resolve the IP to this name? – HaBo Mar 10 '15 at 16:59
  • Yes, the best thing to do would be to use hostnames, in both your connection string AND in your replica set config. But those hostnames have to be resolvable from your client. As you said, they currently aren't which is why you are having issues. – Craig Wilson Mar 10 '15 at 18:41
  • will give it a try and see if that would fix it. But again i don't understand why does it work perfectly if my connection string is not for replica set. – HaBo Mar 10 '15 at 18:51
  • It works fine if the connection string isn't for a replica set because when there isn't a replica set, there isn't a replica set config for us to use. Hence, the only source of data when not using a replica set is the connection string. – Craig Wilson Mar 11 '15 at 03:14
  • You were right. That was it. changed host names to public domain and updated the connection string and so far working good. – HaBo Mar 12 '15 at 00:31