Failed to connect to replica set member matching primary reading preference

I have 3 mongodb nodes: primary, secondary and arbiter (version 2.4.9)

I have a mongodb driver C # 1.8.3 I use the following connection string:

connection string "mongodb: // mongo2, mongo1, mongo3 /? connect = replicaset & replicaset = myrs & readPreference = SecondaryPreferred"

When recording a half case, the driver throws an exception:

Unable to connect to replica set member matching primary reading preference

My code is:

var client = new MongoClient(connectionString); var server = client.GetServer(); var database = server.GetDatabase(dbName); var collection = database.GetCollection<T>(collectionName); collection.Insert(newDoc); 

What am I doing wrong?

+6
source share
3 answers

I suspect this because you specified the wrong hostnames.

Here is one thing you should know, the list of available mongo instances does not come from your connection string, it comes from the return result of the first available mongo instance. For example, when a driver interacts with an instance of mongo1, it gets a list of available instances, which you can get with the following command:

 rs.status() 

in my laptop it returns something like this:

 { "set" : "rs0", "date" : ISODate("2014-01-27T06:43:11Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "YX-ARCH:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 15894, "optime" : Timestamp(1390804960, 1), "optimeDate" : ISODate("2014-01-27T06:42:40Z"), "self" : true }, { "_id" : 1, "name" : "YX-ARCH:27011", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 31, "optime" : Timestamp(1390804960, 1), "optimeDate" : ISODate("2014-01-27T06:42:40Z"), "lastHeartbeat" : ISODate("2014-01-27T06:43:10Z"), "lastHeartbeatRecv" : ISODate("2014-01-27T06:43:10Z"), "pingMs" : 0, "syncingTo" : "YX-ARCH:27017" } ], "ok" : 1 } 

This means that there are two instances of YX-ARCH: 27017 (primary) and YX-ARCH: 27011 (secondary).

Now the point is that these host names must be resolvable on your IIS server, because your driver will use these addresses to connect to mongo instances.

Thus, if the host name is resolved for Internet IP, while your mongo service is only available on the intranet, you can never connect to it. And you will get the error above.

One more thing: it is not recommended to create a new instance of MongoClient each time. From the document, you can know this is a thread-safe class. And MongoClient is added to the driver for managing a set of replicas. Therefore, in my opinion, you should keep it in a single copy of the object. Since every time you create a new instance, it tries to get replica set settings from one instance, which is not very good for efficiency.

+5
source
 What am I doing wrong? 

I think you should not declare an arbiter in the connection string. The arbiter is not really a node, it does not contain any data, its task is to vote on the primary node (the one who takes the records) of the replicaSet.

You have successfully added an arbiter, because without it the nodes (and voices) were even, and in any case you would encounter some cases of connection failure. In any case, your problem is not related to this. Do you really need to connect=replicaset ?

I don’t know much about C #, but I read that the connection strings have the same format for all official drivers, also this is an example in php:

  $m = new MongoClient("mongodb://mongo1:27017,mongo2:27018/?replicaSet=myrs&readPreference=secondary"); 

Are you checking for possible consistency? let me know what you think about it, I did not have time to check it

they don’t know if its case is case sensitive, but pay attention to the capital letter replicaSet=myrs

0
source

I know that the mail is out of date, but I came across this question last week and I was able to solve the problem.

A few things you should check:

  • If your connection to mongodb is hosted in your connectionstrings.config, & should be &amp; otherwise you will get an "entity problem".
  • Do not forget to add the mongodb port 27017
  • Use primaryPreffered over secondaryPreferred - this is if you want the application to be read from the primary under normal circumstances, but to allow obsolete reads from the secondary when the primary is unavailable. This provides a "read-only mode" for your application during the transition to another resource. Using secondaryPreferred is a contraindication. Find out more from mongodb's official documentation here .

  • It’s obvious to check if the replica set is related to each other, which means there is a member of the arbiter, so you ran into this problem. I would like to bet if you just placed mongo1 and deleted the other replica set options, this would work just fine.

  • As pointed out by yaoxing, if you are using host / name mongodb, make sure they are correct, further testing is to use the MongoDB IP address instead of the host name / name, which will still work according to my testing.
  • Mongodb parameters are case sensitive, so use replicaSet not replicaSet as indicated by lese.
  • If you have an arbiter member, you do not need to include in your connection. Fur
  • Alternatively, you can check out this configuration MongoDB article . It also includes encryption in case you want it, but in general, if all the points have been addressed, you should not have this problem.

Tell me.

- Marvin

0
source

All Articles