6

I have 2 application servers, which are connecting to a replicaSet (Primary, Secondary and Arbitrer).

Issue i'm facing is

 [ 'MongoError: no primary found in replicaset',
  '    at ../server/node_modules/mongodb-core/lib/topologies/replset.js:524:28',
  '    at null.<anonymous> (../server/node_modules/mongodb-core/lib/topologies/replset.js:303:24)',
  '    at g (events.js:260:16)',
  '    at emitOne (events.js:77:13)',
  '    at emit (events.js:169:7)',
  '    at null.<anonymous> (../server/node_modules/mongodb-core/lib/topologies/server.js:326:21)',
  '    at emitOne (events.js:77:13)',
  '    at emit (events.js:169:7)',
  '    at null.<anonymous> (../server/node_modules/mongodb-core/lib/connection/pool.js:270:12)',
  '    at g (events.js:260:16)',
  '    at emitTwo (events.js:87:13)',
  '    at emit (events.js:172:7)',
  '    at Socket.<anonymous> (../server/node_modules/mongodb-core/lib/connection/connection.js:175:49)',
  '    at Socket.g (events.js:260:16)',
  '    at emitOne (events.js:77:13)',
  '    at Socket.emit (events.js:169:7)',
  '    at connectErrorNT (net.js:996:8)',
  '    at nextTickCallbackWith2Args (node.js:442:9)',
  '    at process._tickCallback (node.js:356:17)' ]

ReplicaSet config on application :

 "mongodb" : { 
      "replicaset": {
       "db"       : "test",
       "user"     : "admin",
       "password" : "*********",
       "name":"rs1",  
       "replicas": [{"host":"App1Box.dmz.mytest.com.au","port":27017}, {"host":"App2Box.dmz.mytest.com.au","port":27018}]  
      } 

rs.status() output

rs1:PRIMARY> rs.status()
{
        "set" : "rs1",
        "date" : ISODate("2018-05-17T03:50:01Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 2,
                        "name" : "App3Box:27018",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 7180,
                        "lastHeartbeat" : ISODate("2018-05-17T03:50:00Z"),
                        "lastHeartbeatRecv" : ISODate("2018-05-17T03:50:00Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 3,
                        "name" : "App2Box:27018",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 7528,
                        "optime" : Timestamp(1526521846, 1),
                        "optimeDate" : ISODate("2018-05-17T01:50:46Z"),
                        "electionTime" : Timestamp(1526521798, 1),
                        "electionDate" : ISODate("2018-05-17T01:49:58Z"),
                        "self" : true
                },
                {
                        "_id" : 4,
                        "name" : "App1Box:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 7139,
                        "optime" : Timestamp(1526521846, 1),
                        "optimeDate" : ISODate("2018-05-17T01:50:46Z"),
                        "lastHeartbeat" : ISODate("2018-05-17T03:50:01Z"),
                        "lastHeartbeatRecv" : ISODate("2018-05-17T03:50:01Z"),
                        "pingMs" : 0,
                        "syncingTo" : "App2Box:27018"
                }
        ],
        "ok" : 1
}

However, I'm seeing this only from one of the app server which is connecting to MongoDB say App1Box. I'm not seeing this issue on App2Box.

I've tried removing members from replicaSets and re-added, issue still exists.

Mongo version : 2.6.8 node version : 4.4.3 npm version : 3.8.9

I can see all the members in replicaSet from mongo console while performing rs.status() on primary and secondary.

Thanks for your help.

dragonfly163
  • 111
  • 1
  • 1
  • 8
  • Show the connection string being used from your node application. Also show the `rs.status()` and check you can actually reach the hostnames defined in the `rs.status()` output from the client. You either are not using the `replicaSet` option in your application connection or your replicaset is misconfigured with "internal" hostnames only, which are not visible from your client application. – Neil Lunn May 17 '18 at 02:51
  • Added the information to the post. – dragonfly163 May 17 '18 at 03:55
  • That config is not a valid connection string. Show the actual call to `MongoClient.connect()` and how you sending through any part of that object. Also I see `"App1Box.dmz.mytest.com.au"` and your `rs.status()` shows just `App1Box`. I presume if you just `ping App1Box` without the FQDN then you get nothing. The config on the replica sets is what the driver uses and not the "seed list" which is a different thing. – Neil Lunn May 17 '18 at 04:18
  • See [Ensuring Your Connection String is Valid for Replica Sets](http://mongodb.github.io/node-mongodb-native/3.0/reference/connecting/connection-settings/#ensure-your-connection-string-is-valid-for-replica-sets) in the driver reference documentation. – Neil Lunn May 17 '18 at 04:20
  • Thanks for your help. Now after changing the seed list to a fully qualified DNS, I don't see this issue happening. – dragonfly163 May 17 '18 at 04:23
  • You're not listening. The "seed list" does not matter here. It's the "config" on the replica set itself that needs to change. And you still are not showing the connection string you are issuing. – Neil Lunn May 17 '18 at 04:24
  • Yeah, changed the replicaSet config to use full DNS name. – dragonfly163 May 17 '18 at 04:26
  • Thank you @NeilLunn for the solution. Have been scouring the internet for this. – Souvik Dey Jul 03 '19 at 09:17

2 Answers2

2

Found the issue to be not using fully qualified DNS names, while adding members to replicaSet. Thanks to @Neil Lunn.

dragonfly163
  • 111
  • 1
  • 1
  • 8
0

1)Please check your connection string contain all DB sever names following bellow link https://docs.mongodb.com/manual/reference/connection-string/#standard-connection-string-format

2) Do a fail-over To do that log into primary node and execute below command

rs.stepDown()
Chamaz
  • 160
  • 1
  • 7