发布于 2015-09-14 14:57:49 | 204 次阅读 | 评论: 0 | 来源: 网络整理

Overview

This tutorial explains how to add an additional member to an existing replica set.

Before adding a new member, see the Adding Members topic in the 副本集的操作和管理 document.

For background on replication deployment patterns, see the 副本集的架构和部署模式 document.

Requirements

  1. An active replica set.
  2. A new MongoDB system capable of supporting your dataset, accessible by the active replica set through the network.

If neither of these conditions are satisfied, please use the MongoDB installation tutorial and the 部署一个副本集 tutorial instead.

Procedures

The examples in this procedure use the following configuration:

  • The active replica set is rs0.
  • The new member to be added is mongodb3.example.net.
  • The mongod instance default port is 27017.
  • The mongodb.conf configuration file exists in the /etc directory and contains the following replica set information:
port = 27017

bind_ip = 10.8.0.10

dbpath = /srv/mongodb/

logpath = /var/log/mongodb.log

fork = true

replSet = rs0

For more information on configuration options, see 配置文件选项.

Add a Member to an Existing Replica Set

This procedure uses the above example configuration.

  1. Deploy a new mongod instance, specifying the name of the replica set. You can do this one of two ways:

    • Using the mongodb.conf file. On the primary, issue a command that resembles the following:

      mongod --config /etc/mongodb.conf
      
    • Using command line arguments. On the primary, issue command that resembles the following:

      mongod --replSet rs0
      

    Take note of the host name and port information for the new mongod instance.

  2. Open a mongo shell connected to the replica set’s primary:

    mongo
    

    注解

    The primary is the only member that can add or remove members from the replica set. If you do not know which member is the primary, log into any member of the replica set using mongo and issue the db.isMaster() command to determine which member is in the isMaster.primary field. For example:

    mongo mongodb0.example.net
    
    db.isMaster()
    

    If you are not connected to the primary, disconnect from the current client and reconnect to the primary.

  3. In the mongo shell, issue the following command to add the new member to the replica set.

    rs.add("mongodb3.example.net")
    

    注解

    You can also include the port number, depending on your setup:

    rs.add("mongodb3.example.net:27017")
    
  4. Verify that the member is now part of the replica set by calling the rs.conf() method, which displays the replica set configuration:

    rs.conf()
    

    You can use the rs.status() function to provide an overview of replica set status.

Add a Member to an Existing Replica Set (Alternate Procedure)

Alternately, you can add a member to a replica set by specifying an entire configuration document with some or all of the fields in a members sub-documents. For example:

rs.add({_id: 1, host: "mongodb3.example.net:27017", priority: 0, hidden: true})

This configures a hidden member that is accessible at mongodb3.example.net:27017. See host, priority, and hidden for more information about these settings. When you specify a full configuration object with rs.add(), you must declare the _id field, which is not automatically populated in this case.

Production Notes

  • In production deployments you likely want to use and configure a control script to manage this process based on this command.
  • A member can be removed from a set and re-added later. If the removed member’s data is still relatively fresh, it can recover and catch up from its old data set. See the rs.add() and rs.remove() helpers.
  • If you have a backup or snapshot of an existing member, you can move the data files (i.e. /data/db or dbpath) to a new system and use them to quickly initiate a new member. These files must be:
    • clean: the existing dataset must be from a consistent copy of the database from a member of the same replica set. See the 系统备份策略 document for more information.
    • recent: the copy must more recent than the oldest operation in the primary member’s oplog. The new secondary must be able to become current using operations from the primary’s oplog.
  • There is a maximum of seven voting members in any replica set. When adding more members to a replica set that already has seven votes, you must either:
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务