2013-09-10 41 views
0

我正在對我設置的集羣進行一些測試。現在,我有一個三節點的集羣,它有一個主節點,一個從節點和一個仲裁器。如何獲取MongoDB連接字符串以處理陷入困境的節點?

我有一個連接字符串像

mongodb://admin:[email protected]_slave_node,the_master_node 

我的印象是的連接字符串中固有的特點之一是,提供一個以上的主機將引入在客戶端上有一定程度的彈性。我期待,當我拿下the_slave_node的PHP驅動程序應該已經轉移並嘗試連接到the_master_node,但是這似乎並沒有這樣的情況,而是我得到的錯誤:

The MongoCursor object has not been correctly initialized by its constructor 

我知道, MongoClient負責進行初始連接,事實上在代碼中就是這樣。所以這個錯誤是一個跡象表明,MongoClient沒有正確連接,我沒有實現正確的錯誤檢查。然而這是一個不同的問題 -

如果至少有一臺主機啓動並且某些主機關閉,我如何保證MongoClient至少能連接到主機csv中的一臺主機?

謝謝

+0

這不是一個連接問題,這意味着你在你的代碼有錯誤,更具體的,如果它不能連接它會拋出一個異常,說沒有發現候選服務器,當我用我總是得到這個錯誤擴展名錯誤 – Sammaye

+0

所以我做了更多的實驗,並且我認爲我更接近這個問題。我改變連接字符串只指向the_master_node(我刪除the_slave_node),所以現在連接字符串只有一個主機。當奴隸和主人都起來時,它的效果很好。當我取下奴隸時,它開始失敗,有時候給我沒有候選人的錯誤,有時給我一個壞的門戶。要清楚,它是連接字符串中的主,而不是從屬。所以我不明白爲什麼取下奴隸會導致這樣的級聯錯誤。有任何想法嗎? –

+0

嗯這是什麼驅動程序版本?還有什麼是你的閱讀偏好? – Sammaye

回答

0

The MongoCursor object has not been correctly initialized by its constructor

如果你正在構建自己的MongoCursor和覆蓋它的構造函數只應不會發生此錯誤。

這將與例如

class MyCursor extends MongoCursor { 
    function __construct(MongoClient $connection, $ns , array $query = array(), array $fields = array()) { 
    /* Do some work, forgetting to call parent::__construct(....); */ 
    } 
} 

如果您沒有任何擴展類的,那麼這個錯誤是definetly的錯誤發生,你應該report it please :)

How do I guarantee that the MongoClient will connect to at least one of the hosts

至少將將每個數據中心的一個成員放入您的種子列表中。 調整各種超時選項,並計劃主要是關閉的情況下(例如,你應該讀取哪些服務器?)

我懷疑你可能忘了指定「replicaSet」選項,因爲你提到沒有它的連接字符串?

以下片段是我推薦的(隨意調整),特別是當您需要完全一致時(例如始終從主文件讀取)。

<?php 
$seedList = "hostname1:port,hostname2:port"; 
$options = array(
    // If the server is down, don't wait forever 
    "connectTimeoutMS" => 500, 
    // When the server goes down in the middle of operation, don't wait forever 
    "socketTimeoutMS" => 5000, 
    "replicaSet"   => "ReplicasetName", 
    "w"     => "majority", 
    // Don't wait forever for majority write acknowledgment 
    "wtimeout"   => 500, 
    // When the primary goes down, allow reading from secondaries 
    "readPreference"  => MongoClient::RP_PRIMARY_PREFERRED, 
    // When the primary is down, prioritize reading from our local datacenter 
    // If that datacenter is down too, fallback to any server available 
    "readPreferenceTags" => array("dc:is", ""), 
); 
try { 
    $mc = new MongoClient($seedList, $options); 
} catch(Exception $e) { 
    /* I always have some sort of "Skynet"/"Ground control"/"Houston, We have a problem" system to automate taking down (or put into automated maintenance mode) my webservers in case of epic failure.. */ 
    automateMaintenance($e); 
}