2011-09-20 100 views
0

我正在尋找正確的語法來使用replicaSet。我看到了PHP頁面http://php.net/manual/en/mongo.construct.php上的文檔,但我不太確定接下來會發生什麼。MongoDB PHP複製集正確的語法

<?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")); 

?> 

在此之後,我應該寫類似

if($m1){ $mongo = $m1; }else{ $mongo = $m2; } 

這是我用笨之內我現有的類的全部內容。需要將其切換爲支持副本集。

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

回答

0

不,您不明白文檔中的示例。他們是配置副本集的兩種不同方式,並且您只能使用其中的一種。

第一個指定了多個服務器,Mongo將在他管理的第一個服務器上檢測到replicaSet。

第二個例子看起來更簡單,因爲它在單個服務器上檢測到它。它工作得很好,直到你在這裏指定的單個服務器是最後一個。

+0

哇......完全誤讀該文檔:)謝謝! –

+0

甚至會發生最好的;) –