Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymongo unable to connect to primary

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
}
like image 374
Green Cell Avatar asked Aug 04 '16 08:08

Green Cell


2 Answers

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.

like image 178
eoinbrazil Avatar answered Sep 28 '22 05:09

eoinbrazil


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)
like image 33
chf Avatar answered Sep 28 '22 04:09

chf