MongoDB PHP ReplicaSet Proper Syntax
I'm looking for the proper syntax to use a replicaSet. I see the documentation on the PHP page http://php.net/manual/en/mongo.construct.php however I'm not quite sure what happens next.
<?php
// pass a comma-separated list of server names to the constructor
$m1 = new Mongo("mongodb://sf2.example.com,ny1.example.com", array("replicaSet" => "myReplSet"));
// you only need to pass a single seed, the driver will derive the full list and
// find the master from this seed
$m2 = new Mongo("mongodb://ny1.example.com", array("replicaSet" => "myReplSet"));
?>
Following this should I write something like
if( $m1 ){ $mongo = $m1; }else{ $mongo = $m2; }
This is the full content of my existing class that I'm using within codeigniter. Need to switch it to support replica sets.开发者_如何学JAVA
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class CI_Mongo extends Mongo
{
var $db;
function CI_Mongo()
{
// Fetch CodeIgniter instance
$ci = get_instance();
// Load Mongo configuration file
$ci->load->config('mongo');
// Fetch Mongo server and database configuration
$server = config_item('mongo_server');
$dbname = config_item('mongo_dbname');
// Initialise Mongo
if ($server)
{
parent::__construct($server);
}
else
{
parent::__construct();
}
$this->db = $this->$dbname;
}
}
?>
No, you didn't understand the examples in the doc. They are 2 different ways to configure a replicaSet, and you have to use only one of them.
In the first one you specify multiple servers, and Mongo will detect the replicaSet on the first he manages to join.
The second exemple seems simpler, as it detects it on a single server. It works very well, until the single server you are specifying here is THE one that is down.
精彩评论