Answer the question
In order to leave comments, you need to log in
MONGO: Why doesn't the connection immediately switch to the second server if the first one fails?
There are three mongo servers: PRIMARY, SECONDARY and ARBITER
pkrs:PRIMARY> rs.status()
{
"set" : "pkrs",
"date" : ISODate("2014-04-09T05:44:14Z"),
"myState" : 1,
"members" : [
{
" _id" : 0,
"name" : "mongodb01:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 93,
"optime" : Timestamp(1397022230, 2 ),
"optimeDate" : ISODate("2014-04-09T05:43:50Z"),
"self" : true
},
{
"_id" : 1,
"name" : "mongodb02:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 14,
"optime" : Timestamp(1397022230, 2),
"optimeDate" : ISODate("2014-04-09T05:43:50Z"),
"lastHeartbeat" : ISODate("2014-04-09T05:44:14Z"),
"lastHeartbeatRecv" : ISODate("2014- 04-09T05:44:13Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "syncing to: mongodb01:27017",
"syncingTo" : "mongodb01:27017"
},
{
"_id" : 2,
"name" : "mongodba:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime " : 92,
"lastHeartbeat" : ISODate("2014-04-09T05:44:14Z"),
"lastHeartbeatRecv" : ISODate("2014-04-09T05:44:14Z"),
"pingMs" :0
}
],
"ok" : 1
}
$m = new MongoClient("mongodb://mongodb01,mongodb02,mysql-develop01,mongodba/?replicaSet=pkrs");
$db = $m->main;
$collection = $db->addPrice;
$collection->drop();
pkrs:PRIMARY> rs.status()
{
"set" : "pkrs",
"date" : ISODate("2014-04-09T05:49:17Z"),
"myState" : 1,
"members" : [
{
" _id" : 0,
"name" : "mongodb01:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(1397022230, 2),
"optimeDate" :ISODate("2014-04-09T05:43:50Z"),
"lastHeartbeat" : ISODate("2014-04-09T05:49:16Z"),
"lastHeartbeatRecv" : ISODate("2014-04-09T05:49:04Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "mongodb02:27017",
"health" : 1,
" state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 318,
"optime" : Timestamp(1397022230, 2),
"optimeDate" : ISODate("2014-04-09T05:43:50Z"),
" self": true
},
{
"_id" : 2,
"name" : "mongodba:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 317,
"lastHeartbeat" : ISODate("2014-04-09T05:49 :16Z"),
"lastHeartbeatRecv" : ISODate("2014-04-09T05:49:16Z"),
"pingMs" : 0
}
],
"ok" : 1
}
Fatal error: Uncaught exception 'MongoConnectionException' with message 'No candidate servers found' in /var/www/test/mongo.php on line 6
MongoConnectionException: No candidate servers found in /var/www/test/mongo.php on line 6
Answer the question
In order to leave comments, you need to log in
Replica set members send heartbeats (pings) to each other every two seconds. If a heartbeat does not return within 10 seconds, the other members mark the delinquent member as inaccessible.
docs.mongodb.org/manual/core/replica-set-elections
In short, this is how it should be. It doesn't work instantly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question