翻译或纠错本页面

部署复制集

This tutorial describes how to create a three-member replica set from three existing mongod instances running with access control disabled.

本教程描述了如何用3台已有的 mongod 实例来部署一个由三个节点组成的 复制集

概述

由三个节点组成的 复制集 为网络故障或是其他的系统故障提供了足够的冗余。该复制集也有足够的分布式读操作的能力。复制集应该保持奇数个节点,这也就保证了 选举 可以正常的进行。参见 复制集概览 以获得更多有关复制集设计的信息。

我们通常现从一个会成为复制集成员的 mongod 实例开始来配置复制集。然后为复制集新增实例。

要求

在生产环境的部署中,我们应该尽可能将复制集中得节点置于不同的机器上。当使用虚拟机的时候,我们应该将 mongod 实例置于拥有冗余电源和冗余网络的机器上。

在我们部署复制集之前,我们必须在 复制集 的每个机器上安装MongoDB实例。如果我们还没安装MongoDB,请参考 安装指南

Before creating your replica set, you should verify that your network configuration allows communication among all members; i.e. each member must be able to connect to every other member. For instructions on how to check your connection, see Test Connections Between all Members.

部署复制集的注意事项

架构

在生产环境中,我们应该将每个节点部署在独立的机器上,并使用标准的MongoDB端口 27017 。使用 bind_ip 参数来限制访问MongoDB的应用程序的地址。

参见 复制集架构 以获得更多信息。

连通性

确保各个节点之间可以正常通讯,且各个客户端都处于安全的可信的网络环境中。可以考虑以下事项:

  • 建立虚拟的专用网络。确保各个节点之间的流量是在本地网络范围内路由的。(Establish a virtual private network. Ensure that your network topology routes all traffic between members within a single site over the local area network.)

  • Configure access control to prevent connections from unknown clients to the replica set.
  • 配置网络设置和防火墙规则来对将MongoDB的端口仅开放给应用程序,来让应用程序发的进出数据包可以与MongoDB正常交流。

最后请确保复制集各节点可以互相通过DNS或是主机名解析。我们需要配置DNS域名或是设置 /etc/hosts 文件来配置。

配置

在启动的时候指定存储在 /etc/mongodb.conf 或是其他地方中的 配置文件 。并在部署MongoDB之前建立MongoDB数据文件夹。

参见 配置文件选项 以获得更多启动参数配置信息。

详细步骤

The following procedure outlines the steps to deploy a replica set when access control is disabled.

1

Start each member of the replica set with the appropriate options.

For each member, start a mongod and specify the replica set name through the replSet option. Specify any other parameters specific to your deployment. For replication-specific parameters, see Replication Options.

If your application connects to more than one replica set, each set should have a distinct name. Some drivers group replica set connections by replica set name.

The following example specifies the replica set name through the --replSet command-line option:

mongod --replSet "rs0"

You can also specify the replica set name in a configuration file. To start mongod with a configuration file, specify the configuration file’s path with the --config option:

mongod --config <path-to-config>

In production deployments, you can configure a init script to manage this process. Init scripts are beyond the scope of this document.

2

Connect a mongo shell to a replica set member.

For example, to connect to a mongod running on localhost on the default port of 27017, simply issue:

mongo
3

Initiate the replica set.

Use rs.initiate() on one and only one member of the replica set:

rs.initiate()

MongoDB initiates a set that consists of the current member and that uses the default replica set configuration.

4

Verify the initial replica set configuration.

Use rs.conf() to display the replica set configuration object:

rs.conf()

The replica set configuration object resembles the following:

{
   "_id" : "rs0",
   "version" : 1,
   "members" : [
      {
         "_id" : 1,
         "host" : "mongodb0.example.net:27017"
      }
   ]
}
5

Add the remaining members to the replica set.

Add the remaining members with the rs.add() method. You must be connected to the primary to add members to a replica set.

rs.add() can, in some cases, trigger an election. If the mongod you are connected to becomes a secondary, you need to connect the mongo shell to the new primary to continue adding new replica set members. Use rs.status() to identify the primary in the replica set.

The following example adds two members:

rs.add("mongodb1.example.net")
rs.add("mongodb2.example.net")

When complete, you have a fully functional replica set. The new replica set will elect a primary.

6

Check the status of the replica set.

Use the rs.status() operation:

rs.status()