Frage

I setup mongodb replica set with 2 node mongodb1 , mongodb2 and 1 arbiter mongodb3. I use php script connect to replicaset.

<?php
 $conn = new MongoClient("mongodb://admin:123456@mongodb1:27017,mongodb2:27017/?replicaSet=MyRepSet&readPreference=secondary");
  $conn->setReadPreference(MongoClient::RP_SECONDARY, array());

  MongoCursor::$slaveOkay = true;

  $db = $conn->test;
  $collection = $db->items;
  var_dump($collection->findOne());

  $conn->close();
?>

Assum that mongodb1 is primary and mongodb2 is secondary. What i want is with read operations client must first connect to mongodb2 if mongodb2 down then alternative connect to mongodb1.

But the fact that when I discover the mongolog file I see that all operation of client is connect to mongodb1 (primary) not the secondary as I want to.

What I wrong here !!!

War es hilfreich?

Lösung

Nothing, your code is correct.

No need to have : MongoCursor::$slaveOkay = true;

Can you give us an extract of yours logs? In mine, I see the connection accepted on the 2 nodes, but the query is well send to the secondary node.

Andere Tipps

I had check again , before I use command tail -f view the log file and catch the following rows:

[initandlisten] connection accepted from IP:PORT #253 (3 connections now open)

I see this row insert to primary log file so I guess client connect to primary.

Now I use mongostat command and see that query statement on secondary and insert statement on primary.

It is greate !

Thanks you

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top