9

I'm trying to find_one by connecting to my replica set's primary node.

MongoClient(hostname, replicaSet="rs0", read_preference=ReadPreference.PRIMARY)

But it results in an error:

ServerSelectionTimeoutError: No replica set members match selector "Primary()"

I'm able to successfully read using SECONDARY_PREFERRED. I also tried connecting using MongoReplicaSetClient with no success. I'm guessing this due to bad configuration, but what should I be looking for?

rs.status:

rs0:PRIMARY> rs.conf()
{
       "_id" : "rs0",
       "version" : 111313,
       "protocolVersion" : NumberLong(1),
       "members" : [
               {
                       "_id" : 1,
                       "host" : "ANDROMEDA:27017",
                       "arbiterOnly" : false,
                       "buildIndexes" : true,
                       "hidden" : false,
                       "priority" : 1,
                       "tags" : {

                       },
                       "slaveDelay" : NumberLong(0),
                       "votes" : 1
               },
               {
                       "_id" : 2,
                       "host" : "mongo02.db.com:27017",
                       "arbiterOnly" : false,
                       "buildIndexes" : true,
                       "hidden" : false,
                       "priority" : 0.5,
                       "tags" : {

                       },
                       "slaveDelay" : NumberLong(0),
                       "votes" : 1
               },
               {
                       "_id" : 3,
                       "host" : "mongo03.db.com:27017",
                       "arbiterOnly" : false,
                       "buildIndexes" : true,
                       "hidden" : false,
                       "priority" : 0.5,
                       "tags" : {

                       },
                       "slaveDelay" : NumberLong(0),
                       "votes" : 1
               }
       ],
       "settings" : {
               "chainingAllowed" : true,
               "heartbeatIntervalMillis" : 2000,
               "heartbeatTimeoutSecs" : 10,
               "electionTimeoutMillis" : 10000,
               "getLastErrorModes" : {

               },
               "getLastErrorDefaults" : {
                       "w" : 1,
                       "wtimeout" : 0
               }
       }
}
rs0:SECONDARY> rs.status()
{
       "set" : "rs0",
       "date" : ISODate("2016-08-04T08:58:02.293Z"),
       "myState" : 2,
       "term" : NumberLong(90),
       "syncingTo" : "mongo03.db.com:27017",
       "heartbeatIntervalMillis" : NumberLong(2000),
       "members" : [
               {
                       "_id" : 1,
                       "name" : "ANDROMEDA:27017",
                       "health" : 1,
                       "state" : 1,
                       "stateStr" : "PRIMARY",
                       "uptime" : 2503,
                       "optime" : {
                               "ts" : Timestamp(1470299746, 1),
                               "t" : NumberLong(90)
                       },
                       "optimeDate" : ISODate("2016-08-04T08:35:46Z"),
                       "lastHeartbeat" : ISODate("2016-08-04T08:58:01.109Z"),
                       "lastHeartbeatRecv" : ISODate("2016-08-04T08:58:01.803Z"),
                       "pingMs" : NumberLong(28),
                       "electionTime" : Timestamp(1469600522, 1),
                       "electionDate" : ISODate("2016-07-27T06:22:02Z"),
                       "configVersion" : 111313
               },
               {
                       "_id" : 2,
                       "name" : "mongo02.db.com:27017",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 7604104,
                       "optime" : {
                               "ts" : Timestamp(1470299746, 1),
                               "t" : NumberLong(90)
                       },
                       "optimeDate" : ISODate("2016-08-04T08:35:46Z"),
                       "syncingTo" : "mongo03.db.com:27017",
                       "configVersion" : 111313,
                       "self" : true
               },
               {
                       "_id" : 3,
                       "name" : "mongo03.db.com:27017",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 2503,
                       "optime" : {
                               "ts" : Timestamp(1470299746, 1),
                               "t" : NumberLong(90)
                       },
                       "optimeDate" : ISODate("2016-08-04T08:35:46Z"),
                       "lastHeartbeat" : ISODate("2016-08-04T08:58:01.948Z"),
                       "lastHeartbeatRecv" : ISODate("2016-08-04T08:58:01.802Z"),
                       "pingMs" : NumberLong(28),
                       "syncingTo" : "ANDROMEDA:27017",
                       "configVersion" : 111313
               }
       ],
       "ok" : 1
}
Community
  • 1
  • 1
Green Cell
  • 4,677
  • 2
  • 18
  • 49
  • Can you show your replica set configuration? – styvane Aug 04 '16 at 11:38
  • @Styvane I updated with `rs.status`. Hope that gives you more info. – Green Cell Aug 04 '16 at 15:40
  • 1
    Have you tried checking that the host name for ANDROMEDA:27017 is resolvable from the host you are trying to connect your application from? It looks like this is a good candidate to start investigating as your secondary hosts all appear to have FQDNs. – eoinbrazil Aug 09 '16 at 13:32
  • @eoinbrazil I wasn't getting a response from ANDROMEDA when trying to ping it! It seems to be working ok. Now if you would kindly make this into an answer I will reward you with the bounty :) – Green Cell Aug 10 '16 at 03:24
  • Btw is there a way from the client side to query what node it's currently reading from? Just want to see if read preference is connecting to the right one. – Green Cell Aug 10 '16 at 03:29
  • Off the top of my head I'm not sure, perhaps the server_info() command in PyMongo otherwise I think you would need to use https://api.mongodb.com/python/current/api/pymongo/monitoring.html a customer listener. Hope it helps and it's a good question! – eoinbrazil Aug 10 '16 at 14:06

2 Answers2

3

In cases with an error message similar to ServerSelectionTimeoutError: No replica set members match selector "Primary()" and where replica set status function output, rs.status(), shows the Primary member using a non-FQDN (ANDROMEDA:27017 as in this case) then it is highly likely the application is unable to resolve the Primary host on the network.

You can easily check this using the following commands from the command line of the host running your application:

$ dig ANDROMEDA
$ ping ANDROMEDA
$ mongo --host ANDROMEDA:27017

If you don't have the Mongo Shell installed on the host running your application you can use Telnet instead.

$ telnet ANDROMEDA 27017

These outputs will allow you to check connectivity between your application host and your mongod host to determine if this is causing the problem.

aitorhh
  • 2,331
  • 1
  • 23
  • 35
eoinbrazil
  • 425
  • 3
  • 8
1

I use this connection string:

 MongoClient('mongodb://mongo01.db.com:27017,mongo02.db.com:27017,mongo03.db.com:27017/mydb',replicaSet="rs0", read_preference=ReadPreference.PRIMARY)
chf
  • 743
  • 4
  • 14