Overview概述
Three member replica sets provide enough redundancy to survive most network partitions and other system failures. 三成员副本集提供了足够的冗余,可以在大多数网络分区和其他系统故障中幸存下来。These sets also have sufficient capacity for many distributed read operations. 这些集合也有足够的容量用于许多分布式读取操作。Replica sets should always have an odd number of members. 副本集的成员数应始终为奇数。This ensures that elections will proceed smoothly. For more about designing replica sets, see the Replication overview.这将确保选举顺利进行。有关设计复制副本集的更多信息,请参阅复制概述。
This page covers how to deploy a replica set for self-managed deployments.本页介绍如何为自我管理部署部署副本集。
To learn more about deploying a replica set for deployments hosted in MongoDB Atlas, see Create a Cluster.要了解有关为MongoDB Atlas中托管的部署部署部署副本集的更多信息,请参阅创建集群。
Requirements需求
For production deployments, you should maintain as much separation between members as possible by hosting the 对于生产部署,您应该通过在单独的机器上托管mongod instances on separate machines. mongod实例来尽可能多地保持成员之间的分离。When using virtual machines for production deployments, you should place each 当使用虚拟机进行生产部署时,您应该将每个mongod instance on a separate host server serviced by redundant power circuits and redundant network paths.mongod实例放置在由冗余电源电路和冗余网络路径服务的单独主机服务器上。
Before you can deploy a replica set, you must install 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,请参阅安装教程。
Considerations When Deploying a Replica Set部署副本集时的注意事项
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上侦听。
Note
Outside of a rolling upgrade, all 除了滚动升级之外,副本集的所有mongod members of a replica set should use the same major version of MongoDB.mongod成员都应该使用相同的MongoDB主版本。
For more information, see Replica Set Deployment Architectures.有关更多信息,请参阅副本集部署体系结构。
Hostnames
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用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 fail startup validation and do 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在配置的地址上监听来自应用程序的连接。
Warning
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 for Self-Managed Deployments. 有关安全建议的完整列表,请参阅自我管理部署的安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证并加强网络基础设施。
MongoDB binaries, MongoDB二进制文件mongod and mongos, bind to localhost by default. mongod和mongos默认绑定到localhost。If the 如果为二进制文件设置了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地址。
By default 默认情况下,绑定到localhost的mongod and mongos that are bound to localhost only accept connections from clients that are running on the same computer. mongod和mongos只接受来自在同一台计算机上运行的客户端的连接。This binding behavior includes 此绑定行为包括mongosh and other members of your replica set or sharded cluster. Remote clients cannot connect to binaries that are bound only to localhost.mongosh和副本集或分片集群的其他成员。远程客户端无法连接到仅绑定到localhost的二进制文件。
To override the default binding and bind to other IP addresses, use the 要覆盖默认绑定并绑定到其他IP地址,请使用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地址列表。
Warning
Starting in MongDB 5.0, split horizon DNS nodes that are only configured with an IP address fail startup validation and report an error. See 从MongDB 5.0开始,仅配置了IP地址的拆分水平DNS节点无法通过启动验证并报告错误。请参阅禁用disableSplitHorizonIPCheck.disableSplitHorizonIPCheck。
For example, the following 例如,以下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
In order to connect to this instance, remote clients must specify the hostname or its associated IP address 为了连接到此实例,远程客户端必须指定主机名或其关联的IP地址198.51.100.1:198.51.100.1:
mongosh --host My-Example-Associated-Hostname
mongosh --host 198.51.100.1Connectivity连接性
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. See the IP Binding considerations.配置网络和防火墙规则,使传入和传出的数据包只允许在默认的MongoDB端口上,并且只允许来自部署内部。请参阅IP绑定注意事项。
Ensure that each member of a replica set is accessible by way of resolvable DNS or hostnames. You should either configure your DNS names appropriately or set up your systems' 确保副本集的每个成员都可以通过可解析的DNS或主机名访问。您应该适当地配置DNS名称,或者设置系统的/etc/hosts file to reflect this configuration./etc/hosts文件以反映此配置。
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 Self-Managed Configuration File Options.有关配置选项的更多信息,请参阅自我管理配置文件选项。
Deploy a Replica Set in the Terminal在终端中部署副本集
This tutorial describes how to create a three-member replica set from three existing 本教程介绍如何从禁用访问控制的三个现有mongod instances running with access control disabled.mongod实例创建三成员副本集。
To deploy a replica set with enabled access control, see Deploy Self-Managed Replica Set With Keyfile Authentication. 要部署启用了访问控制的副本集,请参阅部署具有键文件身份验证的自我管理副本集。If you wish to deploy a replica set from a single MongoDB instance, see Convert a Standalone Self-Managed mongod to a Replica Set. 如果您希望从单个MongoDB实例部署副本集,请参阅将独立自管理MongoDB转换为副本集。For more information on replica set deployments, see the Replication and Replica Set Deployment Architectures documentation.有关副本集部署的更多信息,请参阅复制和副本集部署体系结构文档。
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.replSetNameoption 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.bindIpoption 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实例与以下主机相关联:
| Hostname | |
|---|---|
| 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绑定:
Warning
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist for Self-Managed Deployments. 在将实例绑定到可公开访问的IP地址之前,必须保护集群免受未经授权的访问。有关安全建议的完整列表,请参阅自我管理部署的安全检查表。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脚本来管理此进程。初始化脚本超出了本文档的范围。
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 要连接到默认端口mongod listening to localhost on the default port of 27017, simply issue:27017上监听localhost的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()。
Important
Run 仅对副本集的一个rs.initiate() on only one mongod instance for the replica set.mongod实例运行rs.initiate()。
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用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 fail startup validation and do 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" : Long(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : Long(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : Long(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : Long(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("585ab9df685f726db2c6a840")
}
}Ensure that the replica set has a primary.确保副本集有一个主副本。
Use 使用rs.status() to identify the primary in the replica set.rs.status()来标识副本集中的主副本。