dqwcdqs358367 2011-09-20 18:13
浏览 61
已采纳

MongoDB PHP ReplicaSet适当的语法

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.

<?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;
    }
} 
?>
  • 写回答

1条回答 默认 最新

  • dpspn60064 2011-09-20 19:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器