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:[email protected]: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:[email protected]: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"
        }
    ]
}
7
  • 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 &amp;'s in your connection uri instead of & Commented Mar 9, 2015 at 17:34
  • We need to see your replica set config. Could you post the results of this from the shell? rs.config(); Commented Mar 9, 2015 at 18:25
  • @CraigWilson added the config info Commented Mar 9, 2015 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? Commented Mar 9, 2015 at 23:46
  • Yes. I confirm. Because it works when I give my connection string as <add key="MongoDBWriteCS" value="mongodb://username:[email protected]:27019/admin?w=0" /> and other two ports as well Commented Mar 10, 2015 at 2:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
IPs keep changing right. isn't this the right way to keep it like a name and resolve the IP to this name?
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.