Docs 菜单

部署地理位置冗余的自管理副本集

This tutorial outlines the process for deploying a 复制集 with members in multiple locations. The tutorial addresses three-member replica sets and five-member replica sets. If you have an even number of replica set members, add another data bearing member, if possible, to deploy an odd number of voting members. [1]

For more information on distributed replica sets, see 跨两个或多个数据中心分布的副本集. See also 副本集部署体系架构 and see 自管理复制参考.

[1](1, 2) If circumstances prohibit another data bearing member and you have an even number of voting members, you can add an arbiter instead. For considerations when using an arbiter, see 副本集投票节点.

在生产中,将副本集的每个成员部署到其自己的计算机上。如果可能,请确保 MongoDB 监听默认端口 27017

注意

在滚动升级之外, 副本集的所有mongod 节点都应使用相同的 MongoDB 主版本。

有关详细信息,请参阅《副本集部署架构》

重要

要避免因 IP 地址变更而更新配置,请使用 DNS 主机名而非 IP 地址。在配置副本集成员或分片集群成员时,使用 DNS 主机名而非 IP 地址尤为重要。

在水平分割网络配置下,请使用主机名而非 IP 地址来配置集群。从 MongoDB 5.0 开始,仅配置了 IP 地址的节点将无法通过启动验证,因而不会启动。

使用 --bind_ip 选项确保 MongoDB 在配置地址监听来自应用程序的连接。

警告

将实例绑定到可公开访问的 IP 地址之前,必须保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自管理部署的安全清单。至少应考虑启用身份验证强化网络基础设施。

MongoDB 二进制文件 mongodmongos 默认绑定到本地主机。如果为此二进制文件设置了 net.ipv6 配置文件设置或 --ipv6 命令行选项,则该二进制文件还会绑定到本地主机 IPv6 地址。

默认情况下,绑定到本地主机的 mongodmongos 只接受来自同一计算机上运行的客户端的连接。这种绑定行为包括 mongosh 和副本集或分片集群的其他节点。远程客户端无法连接到仅绑定到本地主机的二进制文件。

要覆盖默认绑定并绑定到其他 IP 地址,请使用 net.bindIp 配置文件设置或 --bind_ip 命令行选项来指定主机名或 IP 地址的列表。

警告

从 MongoDB 5.0 开始,仅配置了 IP 地址的水平分割 DNS 节点无法通过启动验证,且会报告错误。请参阅 disableSplitHorizonIPCheck

例如,以下 mongod 实例会绑定到本地主机和主机名 My-Example-Associated-Hostname,而该主机名与 IP 地址 198.51.100.1 相关联:

mongod --bind_ip localhost,My-Example-Associated-Hostname

为了连接到此实例,远程客户端必须指定主机名或其关联的 IP 地址 198.51.100.1

mongosh --host My-Example-Associated-Hostname
mongosh --host 198.51.100.1

确保网络流量能在集中的所有节点和网络中的所有客户端之间安全传递。

请考虑以下内容:

  • 建立虚拟专用网络。确保网络拓扑通过局域网路由单个站点内节点之间的所有流量。

  • 配置访问控制,防止未知客户端连接到副本集。

  • 配置网络连接和防火墙规则,以便仅允许在默认 MongoDB 端口上且仅来自部署内部的传入和传出数据包。请参阅《IP 绑定考虑因素》。

确保副本集中的每个成员都可以通过可解析的 DNS 或主机名进行访问。您应适当配置 DNS 名称或设置系统的 /etc/hosts 文件,以反映此配置。

每个成员均须能连接到所有其他成员。关于如何检查连接,请参阅《测试所有成员之间的连接》

在部署 MongoDB 之前创建 MongoDB 存储数据文件的目录。

在存储在 /etc/mongod.conf 或相关位置的配置文件中指定 mongod 配置。

有关配置选项的更多信息,请参阅自管理配置文件选项。

If possible, use an odd number of data centers, and choose a distribution of members that maximizes the likelihood that even with a loss of a data center, the remaining replica set members can form a majority or at minimum, provide a copy of your data.

Never deploy more than seven voting members.

For all configurations in this tutorial, deploy each replica set member on a separate system. Although you may deploy more than one replica set member on a single system, doing so reduces the redundancy and capacity of the replica set. Such deployments are typically for testing purposes.

This tutorial assumes you have installed MongoDB on each system that will be part of your replica set. If you have not already installed MongoDB, see the installation tutorials.

重要

要避免因 IP 地址变更而更新配置,请使用 DNS 主机名而非 IP 地址。在配置副本集成员或分片集群成员时,使用 DNS 主机名而非 IP 地址尤为重要。

在水平分割网络配置下,请使用主机名而非 IP 地址来配置集群。从 MongoDB 5.0 开始,仅配置了 IP 地址的节点将无法通过启动验证,因而不会启动。

For a geographically redundant three-member replica set deployment, you must decide how to distribute your system. Some possible distributions for the three members are:

  • Across Three Data Centers: One member to each site.

  • Across Two Data Centers: Two members to Site A and one member to Site B. If one of the members of the replica set is an arbiter [1], distribute the arbiter to Site A with a data-bearing member.

注意

将副本集成员分布在两个数据中心比分布在一个数据中心更有优势。分布在两个数据中心时,

  • 如果其中一个数据中心发生故障,数据仍可供读取,分布在单个数据中心则无法实现此功能。

  • 如果具有少数成员的数据中心出现故障,副本集仍然可以支持写入操作和读取操作。

  • 但是,如果具有大多数成员的数据中心出现故障,副本集将变为只读。

如有可能,请将成员分布在至少三个数据中心。对于配置服务器副本集 (CSRS),最佳实践是分布在三个数据中心(也可根据成员数量来增加数据中心数量)。如果使用第三个数据中心成本过高,一种可行的分布方法是在两个数据中心均匀分配数据承载成员,并将剩余成员存储在云中(如果公司政策允许)。

1

为每个成员启动一个 mongod实例,设置如下:

  • replication.replSetName 选项设置为副本集名称。如果您的应用程序连接到多个副本集,则每个副本集的名称必须不同。

  • net.bindIp 选项设置为主机名/IP,或使用逗号隔开的主机名/IP 列表。

  • 设置适合您的部署的任何其他设置。

在本教程中,三个 mongod 实例与以下主机相关联:

副本集成员
主机名

成员 0

mongodb0.example.net

成员 1

mongodb1.example.net

成员 2

mongodb2.example.net

以下示例通过 --replSet--bind_ip 命令行选项指定副本集名称和 IP 绑定:

警告

将实例绑定到可公开访问的 IP 地址之前,必须保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自管理部署的安全清单。至少应考虑启用身份验证强化网络基础设施。

mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>

对于 <hostname(s)|ip address(es)>,指定您的 mongod 实例的主机名和/或 IP 地址,远程客户端(包括副本集的其他成员)可以使用它们连接到该实例。

或者,您也可以在配置文件中指定 replica set nameip addresses

replication:
replSetName: "rs0"
net:
bindIp: localhost,<hostname(s)|ip address(es)>

要使用配置文件启动 mongod,请使用 --config 选项指定配置文件的路径:

mongod --config <path-to-config>

在生产部署中,您可以配置一个初始化脚本来管理此进程。初始化脚本超出了本文档的范围。

2

在运行 mongod 的同一台机器上(本教程中为 mongodb0.example.net)启动 mongosh。要连接到在默认端口 27017 上监听本地主机的 mongod,只需发出以下命令:

mongosh

您可能需要指定 mongosh 二进制文件的路径,具体取决于您的路径。

如果 mongod 未在默认端口上运行,请为 mongosh 指定 --port 选项。

3

通过 mongosh,在副本集成员 0 上运行 rs.initiate()

重要

仅在副本集的一个 mongod 实例上运行 rs.initiate()

重要

要避免因 IP 地址变更而更新配置,请使用 DNS 主机名而非 IP 地址。在配置副本集成员或分片集群成员时,使用 DNS 主机名而非 IP 地址尤为重要。

在水平分割网络配置下,请使用主机名而非 IP 地址来配置集群。从 MongoDB 5.0 开始,仅配置了 IP 地址的节点将无法通过启动验证,因而不会启动。

rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "mongodb0.example.net:27017" },
{ _id: 1, host: "mongodb1.example.net:27017" },
{ _id: 2, host: "mongodb2.example.net:27017" }
]
})

MongoDB 使用默认副本集配置启动副本集。

4

使用 rs.conf() 显示副本集配置对象

rs.conf()

副本集配置对象与以下内容类似:

{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("585ab9df685f726db2c6a840")
}
}
5

某些情况下,您可能希望一个数据中心内的节点先于其他数据中心内的节点被选为主节点。您可以修改节点的 priority,使一个数据中心中的节点的 priority 高于其他数据中心的节点。

Some members of the replica set, such as members that have networking restraint or limited resources, should not be able to become primary in a 故障切换. Configure members that should not become primary to have priority 0.

For example, to lower the relative eligibility of the member located in one of the sites (in this example, mongodb2.example.net), set the member's priority to 0.5.

  1. View the replica set configuration to determine the members array position for the member.

    注意

    The array position is not the same as the _id.

    rs.conf()
  2. Copy the replica set configuration object to a variable (to cfg in the example below). Then, in the variable, set the correct priority for the member. Pass the variable to rs.reconfig() to update the replica set configuration.

    For example, to set priority for the third member in the array (i.e., the member at position 2), issue the following sequence of commands:

    cfg = rs.conf()
    cfg.members[2].priority = 0.5
    rs.reconfig(cfg)

    注意

    The rs.reconfig() shell method can force the current primary to step down, causing an election. When the primary steps down, all clients disconnect. This is the intended behavior. While median time to elect a new primary should not typically exceed 12 seconds, make sure any replica configuration changes occur during scheduled maintenance periods.

After these commands return, you have a geographically redundant three-member replica set.

6

使用 rs.status() 标识副本集中的主节点。

重要

要避免因 IP 地址变更而更新配置,请使用 DNS 主机名而非 IP 地址。在配置副本集成员或分片集群成员时,使用 DNS 主机名而非 IP 地址尤为重要。

在水平分割网络配置下,请使用主机名而非 IP 地址来配置集群。从 MongoDB 5.0 开始,仅配置了 IP 地址的节点将无法通过启动验证,因而不会启动。

For a geographically redundant five-member replica set deployment, you must decide how to distribute your system. Some possible distributions for the five members are:

  • Across Three Data Centers: Two members in Site A, two members in Site B, one member in Site C.

  • Across Four Data Centers: Two members in one site, and one member in the other three sites.

  • Across Five Data Centers: One member in each site.

  • Across Two Data Centers: Three members in Site A and two members in Site B. If possible, avoid distributing config server replica set across only two data centers.

注意

将副本集成员分布在两个数据中心比分布在一个数据中心更有优势。分布在两个数据中心时,

  • 如果其中一个数据中心发生故障,数据仍可供读取,分布在单个数据中心则无法实现此功能。

  • 如果具有少数成员的数据中心出现故障,副本集仍然可以支持写入操作和读取操作。

  • 但是,如果具有大多数成员的数据中心出现故障,副本集将变为只读。

如有可能,请将成员分布在至少三个数据中心。对于配置服务器副本集 (CSRS),最佳实践是分布在三个数据中心(也可根据成员数量来增加数据中心数量)。如果使用第三个数据中心成本过高,一种可行的分布方法是在两个数据中心均匀分配数据承载成员,并将剩余成员存储在云中(如果公司政策允许)。

1

为每个成员启动一个 mongod实例,设置如下:

  • replication.replSetName option to the replica set name,

    如果您的应用程序连接到多个副本集,则每个副本集的名称必须不同。有些驱动程序会按副本集名称对副本集连接进行分组。

  • net.bindIp option to the hostname/ip address or a comma-delimited list of hostnames/ip addresses, and

  • 设置适合您的部署的任何其他设置。

In this tutorial, the five mongod instances are associated with the following hosts:

副本集成员
主机名

成员 0

mongodb0.example.net

成员 1

mongodb1.example.net

成员 2

mongodb2.example.net

Member 3

mongodb3.example.net

Member 4

mongodb4.example.net

以下示例通过 --replSet--bind_ip 命令行选项指定副本集名称和 IP 绑定:

警告

将实例绑定到可公开访问的 IP 地址之前,必须保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自管理部署的安全清单。至少应考虑启用身份验证强化网络基础设施。

mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>

对于 <hostname(s)|ip address(es)>,指定您的 mongod 实例的主机名和/或 IP 地址,远程客户端(包括副本集的其他成员)可以使用它们连接到该实例。

Alternatively, you can also specify the replica set name and the hostnames/ip addresses in a 配置文件:

replication:
replSetName: "rs0"
net:
bindIp: localhost,<hostname(s)|ip address(es)>

要使用配置文件启动 mongod,请使用 --config 选项指定配置文件的路径:

mongod --config <path-to-config>

在生产部署中,您可以配置一个初始化脚本来管理此进程。初始化脚本超出了本文档的范围。

2

在运行 mongod 的同一台机器上(本教程中为 mongodb0.example.net)启动 mongosh。要连接到在默认端口 27017 上监听本地主机的 mongod,只需发出以下命令:

mongosh

您可能需要指定 mongosh 二进制文件的路径,具体取决于您的路径。

如果 mongod 未在默认端口上运行,请为 mongosh 指定 --port 选项。

3

通过 mongosh,在副本集成员 0 上运行 rs.initiate()

重要

仅在副本集的一个 mongod 实例上运行 rs.initiate()

rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "mongodb0.example.net:27017" },
{ _id: 1, host: "mongodb1.example.net:27017" },
{ _id: 2, host: "mongodb2.example.net:27017" },
{ _id: 3, host: "mongodb3.example.net:27017" },
{ _id: 4, host: "mongodb4.example.net:27017" }
]
})
4

使用 rs.conf() 显示副本集配置对象

rs.conf()

副本集配置对象与以下内容类似:

{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "mongodb3.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 4,
"host" : "mongodb4.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5df2c9ccc21c478b838b98d6")
}
}
5

某些情况下,您可能希望一个数据中心内的节点先于其他数据中心内的节点被选为主节点。您可以修改节点的 priority,使一个数据中心中的节点的 priority 高于其他数据中心的节点。

Some members of the replica set, such as members that have networking restraint or limited resources, should not be able to become primary in a 故障切换. Configure members that should not become primary to have priority 0.

For example, to lower the relative eligibility of the member located in one of the sites (in this example, mongodb2.example.net), set the member's priority to 0.5.

  1. View the replica set configuration to determine the members array position for the member.

    注意

    The array position is not the same as the _id.

    rs.conf()
  2. Copy the replica set configuration object to a variable (to cfg in the example below). Then, in the variable, set the correct priority for the member. Pass the variable to rs.reconfig() to update the replica set configuration.

    For example, to set priority for the third member in the array (i.e., the member at position 2), issue the following sequence of commands:

    cfg = rs.conf()
    cfg.members[2].priority = 0.5
    rs.reconfig(cfg)

    注意

    The rs.reconfig() shell method can force the current primary to step down, causing an election. When the primary steps down, all clients disconnect. This is the intended behavior. While median time to elect a new primary should not typically exceed 12 seconds, make sure any replica configuration changes occur during scheduled maintenance periods.

After these commands return, you have a geographically redundant five-member replica set.

6

使用 rs.status() 标识副本集中的主节点。