MongoDB PHP ReplicaSet适当的语法

I'm looking for the proper syntax to use a replicaSet. I see the documentation on the PHP page however I'm not quite sure what happens next.


// pass a comma-separated list of server names to the constructor
$m1 = new Mongo("mongodb://,", 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://", 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.

<?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

        // Fetch Mongo server and database configuration
        $server = config_item('mongo_server');
        $dbname = config_item('mongo_dbname');

        // Initialise Mongo
        if ($server)
        $this->db = $this->$dbname;


不,你不明白文档中的例子。 它们有两种不同的方式来配置replicaSet,你只需要使用其中一种。</ p>

在第一种中指定多个服务器,Mongo将在第一次检测到replicaSet 设法加入。</ p>

第二个例子似乎更简单,因为它在一台服务器上检测到它。 它运行得很好,直到你在这里指定的单个服务器是一个关闭的服务器。</ p>
</ div>



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.

douzhuang1900 甚至发生了最好的事情;)
大约 9 年之前 回复
dongzong5017 哇...完全误读了那个文档:)谢谢!
大约 9 年之前 回复