我正在寻找使用replicaSet的正确语法。我在PHP页面http://php.net/manual/en/mongo.construct.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; }这是我在codeigniter中使用的现有类的全部内容。需要切换它以支持副本集。
<?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;
}
}
?>发布于 2011-09-21 03:26:27
不,您没有理解文档中的示例。它们是配置replicaSet的两种不同方法,您只需使用其中一种。
在第一个示例中,您指定了多个服务器,Mongo将在他设法加入的第一个服务器上检测replicaSet。
第二个示例看起来更简单,因为它在单个服务器上检测到它。它工作得很好,直到您在这里指定的单个服务器出现故障。
https://stackoverflow.com/questions/7489769
复制相似问题