Deploy a Geographically Redundant Replica Set部署地理位置冗余的复制副本集
On this page本页内容
Overview概述
This tutorial outlines the process for deploying a replica set 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 Replica Sets Distributed Across Two or More Data Centers. 有关分布式副本集的详细信息,请参阅分布在两个或多个数据中心的副本集。See also Replica Set Deployment Architectures and see Replication Reference.另请参阅复制副本集部署体系结构,并参阅复制参考。
[1] | (1, 2) |
Considerations注意事项
Architecture建筑学
In production, deploy each member of the replica set to its own machine. 在生产中,将复制副本集的每个成员部署到自己的计算机上。If possible, ensure that MongoDB listens on the default port of 如果可能,请确保MongoDB在27017
.27017
的默认端口上侦听。
For more information, see Replica Set Deployment Architectures.有关详细信息,请参阅复制副本集部署体系结构。
Hostnames主机名
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.使用主机名而不是IP地址来跨拆分的网络范围配置群集。从MongoDB 5.0开始,只配置了IP地址的节点将无法通过启动验证,也不会启动。
IP BindingIP绑定
Use the 使用--bind_ip
option to ensure that MongoDB listens for connections from applications on configured addresses.--bind_ip
选项可以确保MongoDB在配置的地址上侦听来自应用程序的连接。
Changed in version 3.6.3.6版更改。
mongod
and mongos
, bind to localhost by default. mongod
和mongos
默认绑定到localhost。net.ipv6
configuration file setting or the --ipv6
command line option is set for the binary, the binary additionally binds to the localhost IPv6 address.net.ipv6
配置文件设置或--ipv6
命令行选项,则二进制文件将额外绑定到localhost ipv6地址。mongod
and mongos
that are bound to localhost only accept connections from clients that are running on the same computer. mongod
和mongos
只接受来自在同一台计算机上运行的客户端的连接。mongosh
and other members of your replica set or sharded cluster. mongosh
和复制集或分片集群的其他成员。net.bindIp
configuration file setting or the --bind_ip
command-line option to specify a list of hostnames or IP addresses.net.bindIp
配置文件设置或--bind_ip
命令行选项指定主机名或IP地址列表。disableSplitHorizonIPCheck
.disableSplitHorizonIPCheck
。mongod
instance binds to both the localhost and the hostname My-Example-Associated-Hostname
, which is associated with the IP address 198.51.100.1
: mongod
实例绑定到localhost和主机名My-Example-Associated-Hostname
,后者与IP地址198.51.100.1
关联:mongod --bind_ip localhost,My-Example-Associated-Hostname
198.51.100.1
: 198.51.100.1
:mongosh --host My-Example-Associated-Hostname
mongosh --host 198.51.100.1
Connectivity连通性
Ensure that network traffic can pass securely between all members of the set and all clients in the network .确保网络流量可以在集合的所有成员和网络中的所有客户端之间安全传递。
Consider the following:考虑以下内容:
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.配置访问控制以防止未知客户端连接到副本集。Configure networking and firewall rules so that incoming and outgoing packets are permitted only on the default MongoDB port and only from within your deployment.配置网络和防火墙规则,使传入和传出数据包只能在默认的MongoDB端口上使用,并且只能从部署中使用。See the IP Binding considerations.请参阅IP绑定注意事项。
Ensure that each member of a replica set is accessible by way of resolvable DNS or hostnames. 确保副本集的每个成员都可以通过可解析的DNS或主机名进行访问。You should either configure your DNS names appropriately or set up your systems' 您应该适当地配置DNS名称,或者设置系统的/etc/hosts文件以反映此配置。/etc/hosts
file to reflect this configuration.
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.每个成员都必须能够连接到其他每个成员。有关如何检查连接的说明,请参阅测试所有成员之间的连接。
Configuration配置
Create the directory where MongoDB stores data files before deploying MongoDB.在部署MongoDB之前,创建MongoDB存储数据文件的目录。
Specify the 在存储在mongod
configuration in a configuration file stored in /etc/mongod.conf
or a related location./etc/mongod.conf
或相关位置的配置文件中指定mongod
配置。
For more information about configuration options, see Configuration File Options.有关配置选项的详细信息,请参阅配置文件选项。
Distribution of the Members成员的分配
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.如果可能,请使用奇数个数据中心,并选择成员分布,以最大限度地提高即使丢失数据中心,剩余副本集成员也可以构成多数或至少提供数据副本的可能性。
Voting Members有表决权的成员
Never deploy more than seven voting members.决不能部署超过七名投票成员。
Prerequisites先决条件
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.本教程假设您已经在每个系统上安装了MongoDB,这些系统将成为您的副本集的一部分。如果您还没有安装MongoDB,请参阅安装教程。
Procedures过程
Deploy a Geographically Redundant Three-Member Replica Set部署地理位置冗余的三成员复制副本集
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.使用主机名而不是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.跨两个数据中心:两个成员连接到站点A,一个成员连接站点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.如果副本集的成员之一是仲裁器[1],则将仲裁器与数据承载成员一起分发到站点A。
Distributing replica set members across two data centers provides benefit over a single data center. In a two data center distribution,将复制副本集成员分布在两个数据中心提供了优于单个数据中心的优势。在双数据中心分布中,
If one of the data centers goes down, the data is still available for reads unlike a single data center distribution.如果其中一个数据中心出现故障,则与单个数据中心分布不同,数据仍可用于读取。If the data center with a minority of the members goes down, the replica set can still serve write operations as well as read operations.如果拥有少数成员的数据中心宕机,副本集仍然可以提供写操作和读操作。However, if the data center with the majority of the members goes down, the replica set becomes read-only.但是,如果包含大多数成员的数据中心出现故障,则副本集将变为只读。
If possible, distribute members across at least three data centers. For config server replica sets (CSRS), the best practice is to distribute across three (or more depending on the number of members) centers. 如果可能,将成员分布在至少三个数据中心。对于配置服务器副本集(CSRS),最佳做法是分布在三个(或更多,具体取决于成员数量)中心。If the cost of the third data center is prohibitive, one distribution possibility is to evenly distribute the data bearing members across the two data centers and store the remaining member in the cloud if your company policy allows.如果第三个数据中心的成本过高,一种分发可能性是在两个数据中心之间平均分配数据承载成员,并在公司政策允许的情况下将剩余成员存储在云中。
Start each member of the replica set with the appropriate options.使用适当的选项启动复制副本集的每个成员。
For each member, start a 对于每个成员,使用以下设置启动一个mongod
instance with the following settings:mongod
实例:
Set将replication.replSetName
option to the replica set name. If your application connects to more than one replica set, each set must have a distinct name.replication.replSetName
选项设置为副本集名称。如果应用程序连接到多个复制副本集,则每个复制副本集都必须具有不同的名称。Set将net.bindIp
option to the hostname/ip or a comma-delimited list of hostnames/ips.net.bindIp
选项设置为主机名/ip或以逗号分隔的主机名/ips列表。Set any other settings as appropriate for your deployment.根据部署情况设置任何其他设置。
In this tutorial, the three 在本教程中,三个mongod
instances are associated with the following hosts:mongod
实例与以下主机相关联:
Member 0 | mongodb0.example.net |
Member 1 | mongodb1.example.net |
Member 2 | mongodb2.example.net |
The following example specifies the replica set name and the ip binding through the 以下示例通过--replSet
and --bind_ip
command-line options:--replSet
和--bind_ip
命令行选项指定副本集名称和ip绑定:
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>
For 对于<hostname(s)|ip address(es)>
, specify the hostname(s) and/or ip address(es) for your mongod
instance that remote clients (including the other members of the replica set) can use to connect to the instance.<hostname(s)|ip address(es)>
,指定mongod
实例的主机名和/或ip地址,远程客户端(包括副本集的其他成员)可以用来连接到该实例。
Alternatively, you can also specify the 或者,您也可以在配置文件中指定副本集名称和ip地址:replica set name
and the ip addresses
in a configuration file:
replication:
replSetName: "rs0"
net:
bindIp: localhost,<hostname(s)|ip address(es)>
To start 要使用配置文件启动mongod
with a configuration file, specify the configuration file's path with the --config
option:mongod
,请使用--config
选项指定配置文件的路径:
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.在生产部署中,您可以配置init脚本来管理这个过程。Init脚本超出了本文档的范围。
Connect mongosh
to one of the mongod
instances.将mongosh
连接到其中一个mongod
实例。
mongosh
to one of the mongod
instances.From the same machine where one of the 在运行其中一个mongod
is running (in this tutorial, mongodb0.example.net
), start mongosh
. mongod
的同一台机器上(在本教程中,mongodb0examplenet),启动mongosh
。To connect to the 要连接到默认端口27017上监听localhost的mongod
listening to localhost on the default port of 27017
, simply issue:mongod
,只需发出:
mongosh
Depending on your path, you may need to specify the path to the 根据您的路径,您可能需要指定mongosh
binary.mongosh
二进制文件的路径。
If your 如果您的mongod
is not running on the default port, specify the --port
option for mongosh
.mongod
没有在默认端口上运行,请为mongosh
指定--port
选项。
Initiate the replica set.启动复制副本集。
From 从mongosh
, run rs.initiate()
on replica set member 0.mongosh
中,对副本集成员0运行rs.initiate()
。
Run 仅对副本集的一个且仅对一个rs.initiate()
on just one and only one mongod
instance for the replica set.mongod
实例运行rs.initiate()
。
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.使用主机名而不是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 initiates a replica set, using the default replica set configuration.MongoDB使用默认的副本集配置启动一个副本集。
View the replica set configuration.查看复制副本集配置。
Use 使用rs.conf()
to display the replica set configuration object:rs.conf()
显示副本集配置对象:
rs.conf()
The replica set configuration object resembles the following:复制副本集配置对象类似于以下内容:
{
"_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")
}
}
Optional. 可选的。Configure the member eligibility for becoming primary.配置成为主要成员的资格。
In some cases, you may prefer that the members in one data center be elected primary before the members in the other data centers. 在某些情况下,您可能更希望一个数据中心中的成员在其他数据中心的成员之前被选为主要成员。You can modify the 您可以修改成员的priority
of the members such that the members in the one data center has higher priority
than the members in the other data centers.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 failover. 副本集的某些成员(例如具有网络限制或资源有限的成员)不应成为故障转移中的主要成员。Configure members that should not become primary to have priority 0.将不应成为主要成员的成员配置为具有优先级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
.mongodb2.example.net
)的成员的相对资格,请将该成员的优先级设置为0.5
。
View the replica set configuration to determine the查看复制副本集配置以确定成员的members
array position for the member. Keep in mind the array position is not the same as the_id
:members
数组位置。请记住,数组位置与_id
不同:rs.conf()
Copy the replica set configuration object to a variable (to将复制副本集配置对象复制到一个变量(在下面的示例中复制到cfg
in the example below).cfg
)。Then, in the variable, set the correct priority for the member.然后,在变量中,为成员设置正确的优先级。Then pass the variable to然后将变量传递给rs.reconfig()
to update the replica set configuration.rs.reconfig()
以更新副本集配置。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:例如,要为数组中的第三个成员(即位于位置2的成员)设置优先级,请发出以下命令序列:cfg = rs.conf()
cfg.members[2].priority = 0.5
rs.reconfig(cfg)NoteThers.reconfig()
shell method can force the current primary to step down, causing an election.rs.reconfig()
shell方法可以强制当前主进程退出,从而导致选举。When the primary steps down, all clients will disconnect.当主服务器关闭时,所有客户端都将断开连接。This is the intended behavior. While median time to elect a new primary should not typically exceed 12 seconds, always make sure any replica configuration changes occur during scheduled maintenance periods.这是预期行为。虽然选择新主服务器的中间时间通常不应超过12秒,但始终确保在计划维护期间发生任何复制副本配置更改。
After these commands return, you have a geographically redundant three-member replica set.在这些命令返回后,您就有了一个地理上冗余的三成员复制副本集。
Ensure that the replica set has a primary.请确保复制副本集具有主副本。
Use 使用rs.status()
to identify the primary in the replica set.rs.status()
来标识副本集中的主副本。
Deploy a Geographically Redundant Five-Member Replica Set部署地理位置冗余的五成员复制副本集
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.使用主机名而不是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.跨三个数据中心:站点A中有两个成员,站点B中有两名成员,站点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.跨两个数据中心:站点A中有三个成员,站点B中有两个成员。如果可能,请避免仅在两个数据集中分发配置服务器副本集。
Distributing replica set members across two data centers provides benefit over a single data center. In a two data center distribution,将复制副本集成员分布在两个数据中心提供了优于单个数据中心的优势。在双数据中心分布中,
If one of the data centers goes down, the data is still available for reads unlike a single data center distribution.如果其中一个数据中心出现故障,则与单个数据中心分布不同,数据仍可用于读取。If the data center with a minority of the members goes down, the replica set can still serve write operations as well as read operations.如果拥有少数成员的数据中心宕机,副本集仍然可以提供写操作和读操作。However, if the data center with the majority of the members goes down, the replica set becomes read-only.但是,如果包含大多数成员的数据中心出现故障,则副本集将变为只读。
If possible, distribute members across at least three data centers. For config server replica sets (CSRS), the best practice is to distribute across three (or more depending on the number of members) centers. 如果可能,将成员分布在至少三个数据中心。对于配置服务器副本集(CSRS),最佳做法是分布在三个(或更多,具体取决于成员数量)中心。If the cost of the third data center is prohibitive, one distribution possibility is to evenly distribute the data bearing members across the two data centers and store the remaining member in the cloud if your company policy allows.如果第三个数据中心的成本过高,一种分发可能性是在两个数据中心之间平均分配数据承载成员,并在公司政策允许的情况下将剩余成员存储在云中。
Start each member of the replica set with the appropriate options.使用适当的选项启动复制副本集的每个成员。
For each member, start a 对于每个成员,使用以下设置启动一个mongod
instance with the following settings:mongod
实例:
Set将replication.replSetName
option to the replica set name,replication.replSetName
选项设置为复制集名称,If your application connects to more than one replica set, each set must have a distinct name. Some drivers group replica set connections by replica set name.如果应用程序连接到多个复制副本集,则每个复制副本集都必须具有不同的名称。某些驱动程序按副本集名称对副本集连接进行分组。Set将net.bindIp
option to the hostname/ip address or a comma-delimited list of hostnames/ip addresses, andnet.bindIp
选项设置为主机名/ip地址或以逗号分隔的主机名/ip地址列表,以及Set any other settings as appropriate for your deployment.根据部署情况设置任何其他设置。
In this tutorial, the five 在本教程中,五个mongod
instances are associated with the following hosts:mongod
实例与以下主机相关联:
Member 0 | mongodb0.example.net |
Member 1 | mongodb1.example.net |
Member 2 | mongodb2.example.net |
Member 3 | mongodb3.example.net |
Member 4 | mongodb4.example.net |
The following example specifies the replica set name and the ip binding through the 以下示例通过--replSet
and --bind_ip
command-line options:--replSet
和--bind_ip
命令行选项指定副本集名称和ip绑定:
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>
For 对于<hostname(s)|ip address(es)>
, specify the hostname(s) and/or ip address(es) for your mongod
instance that remote clients (including the other members of the replica set) can use to connect to the instance.<hostname(s)|ip address(es)>
,指定mongod
实例的主机名和/或ip地址,远程客户端(包括副本集的其他成员)可以用来连接到该实例。
Alternatively, you can also specify the 或者,您也可以在配置文件中指定复制集名称和主机名/ip地址:replica set name
and the hostnames/ip addresses
in a configuration file:
replication:
replSetName: "rs0"
net:
bindIp: localhost,<hostname(s)|ip address(es)>
To start 要使用配置文件启动mongod
with a configuration file, specify the configuration file's path with the --config
option:mongod
,请使用--config
选项指定配置文件的路径:
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.在生产部署中,您可以配置init脚本来管理这个过程。Init脚本超出了本文档的范围。
Connect mongosh
to one of the mongod
instances.将mongosh
连接到其中一个mongod
实例。
mongosh
to one of the mongod
instances.From the same machine where one of the 在运行其中一个mongod
is running (in this tutorial, mongodb0.example.net
), start mongosh
. mongod
的同一台机器上(在本教程中,mongodb0.example.net
),启动mongosh
。To connect to the 要连接到默认端口27017上监听localhost的mongod
listening to localhost on the default port of 27017
, simply issue:mongod
,只需发出:
mongosh
Depending on your path, you may need to specify the path to the 根据您的路径,您可能需要指定mongosh
binary.mongosh
二进制文件的路径。
If your 如果您的mongod
is not running on the default port, specify the --port
option for mongosh
.mongod
没有在默认端口上运行,请为mongosh
指定--port
选项。
Initiate the replica set.启动复制副本集。
From 从mongosh
, run rs.initiate()
on replica set member 0.mongosh
中,对副本集成员0运行rs.initiate()
。
Run 仅对副本集的一个且仅对一个rs.initiate()
on just one and only one mongod
instance for the replica set.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" }
]
})
View the replica set configuration.查看复制副本集配置。
Use 使用rs.conf()
to display the replica set configuration object:rs.conf()
显示副本集配置对象:
rs.conf()
The replica set configuration object resembles the following:复制副本集配置对象类似于以下内容:
{
"_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")
}
}
Optional. 可选的。Configure the member eligibility for becoming primary.配置成为主要成员的资格。
In some cases, you may prefer that the members in one data center be elected primary before the members in the other data centers. 在某些情况下,您可能更希望一个数据中心中的成员在其他数据中心的成员之前被选为主要成员。You can modify the 您可以修改成员的priority
of the members such that the members in the one data center has higher priority
than the members in the other data centers.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 failover. 副本集的某些成员(例如具有网络限制或资源有限的成员)不应成为故障转移中的主要成员。Configure members that should not become primary to have priority 0.将不应成为主要成员的成员配置为具有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
.mongodb2.example.net
)的成员的相对资格,请将该成员的优先级设置为0.5
。
View the replica set configuration to determine the查看复制副本集配置以确定成员的members
array position for the member. Keep in mind the array position is not the same as the_id
:members
数组位置。请记住,数组位置与_id
不同:rs.conf()
Copy the replica set configuration object to a variable (to将复制副本集配置对象复制到一个变量(在下面的示例中复制到cfg
in the example below).cfg
)。Then, in the variable, set the correct priority for the member.然后,在变量中,为成员设置正确的优先级。Then pass the variable to然后将变量传递给rs.reconfig()
to update the replica set configuration.rs.reconfig()
以更新副本集配置。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:例如,要为数组中的第三个成员(即位于位置2的成员)设置优先级,请发出以下命令序列:cfg = rs.conf()
cfg.members[2].priority = 0.5
rs.reconfig(cfg)NoteThers.reconfig()
shell method can force the current primary to step down, causing an election. When the primary steps down, all clients will disconnect.rs.reconfig()
shell方法可以强制当前主进程退出,从而导致选举。当主服务器关闭时,所有客户端都将断开连接。This is the intended behavior. While median time to elect a new primary should not typically exceed 12 seconds, always make sure any replica configuration changes occur during scheduled maintenance periods.这是预期行为。虽然选择新主服务器的中间时间通常不应超过12秒,但始终确保在计划维护期间发生任何复制副本配置更改。
After these commands return, you have a geographically redundant five-member replica set.在这些命令返回后,您就有了一个地理上冗余的五成员复制副本集。
Ensure that the replica set has a primary.请确保复制副本集具有主副本。
Use 使用rs.status()
to identify the primary in the replica set.rs.status()
来标识副本集中的主副本。