On this page本页内容
This procedure describes deploying a replica set in a development or test environment. 此过程描述在开发或测试环境中部署副本集。For a production deployment, refer to the Deploy a Replica Set tutorial.有关生产部署,请参阅部署副本集教程。
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 Replica Set With Keyfile Authentication. 要部署启用了访问控制的副本集,请参阅使用密钥文件身份验证部署副本集。If you wish to deploy a replica set from a single MongoDB instance, see Convert a Standalone to a Replica Set. 如果要从单个MongoDB实例部署副本集,请参阅将独立副本集转换为副本集。For more information on replica set deployments, see the Replication and Replica Set Deployment Architectures documentation.有关副本集部署的详细信息,请参阅复制和副本集部署体系结构文档。
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.有关设计副本集的更多信息,请参阅复制概述。
For test and development systems, you can run your 对于测试和开发系统,您可以在本地系统或虚拟实例中运行mongod
instances on a local system, or within a virtual instance.mongod
实例。
Before you can deploy a replica set, you must install MongoDB on each system that will be part of your replica set. 在部署副本集之前,必须在将成为副本集一部分的每个系统上安装MongoDB。If you have not already installed MongoDB, see the installation tutorials.如果尚未安装MongoDB,请参阅安装教程。
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.有关如何检查连接的说明,请参阅测试所有成员之间的连接。
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. 使用主机名而不是IP地址在拆分的网络范围内配置群集。Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅配置IP地址的节点将无法启动验证,并且不会启动。
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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.至少,考虑启用身份验证和强化网络基础设施。
MongoDB binaries, 默认情况下,MongoDB二进制文件mongod
and mongos
, bind to localhost by default. mongod
和mongos
绑定到本地主机。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
命令行选项,则二进制文件还绑定到本地主机IPv6地址。
By default 默认情况下,绑定到mongod
and mongos
that are bound to localhost only accept connections from clients that are running on the same computer. localhost
的mongod
和mongos
只接受来自同一台计算机上运行的客户端的连接。This binding behavior includes 这种绑定行为包括mongosh
and other members of your replica set or sharded cluster. mongosh
和副本集或分片集群的其他成员。Remote clients cannot connect to binaries that are bound only to 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地址列表。
Starting in MongDB 5.0, split horizon DNS nodes that are only configured with an IP address fail startup validation and report an error. 从MongDB 5.0开始,仅配置有IP地址的拆分地平线DNS节点无法启动验证并报告错误。See 请参见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
实例绑定到本地主机和主机名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.1
In this test deployment, the three members run on the same machine.在此测试部署中,三个成员在同一台计算机上运行。
These instructions should only be used for test or development deployments.这些说明只能用于测试或开发部署。
The examples in this procedure create a new replica set named 本过程中的示例将创建一个名为rs0
.rs0
的新副本集。
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.一些驱动程序按副本集名称对副本集连接进行分组。
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. 使用主机名而不是IP地址在拆分的网络范围内配置群集。Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅配置IP地址的节点将无法启动验证,并且不会启动。
Create the necessary data directories for each member by issuing a command similar to the following:通过发出与以下类似的命令,为每个成员创建必要的数据目录:
mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2
This will create directories called "rs0-0", "rs0-1", and "rs0-2", which will contain the instances' database files.这将创建名为“rs0-0”、“rs0-1”和“rs0.2”的目录,其中将包含实例的数据库文件。
Start your 通过发出以下命令,在自己的shell窗口中启动mongod
instances in their own shell windows by issuing the following commands:mongod
实例:
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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.至少,考虑启用身份验证和强化网络基础设施。
First member:第一成员:
mongod --replSet rs0 --port 27017 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-0 --oplogSize 128
Second member:第二成员:
mongod --replSet rs0 --port 27018 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-1 --oplogSize 128
Third member:第三成员:
mongod --replSet rs0 --port 27019 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-2 --oplogSize 128
This starts each instance as a member of a replica set named 这会将每个实例作为名为rs0
, each running on a distinct port, and specifies the path to your data directory with the --dbpath
setting. rs0
的副本集的成员启动,每个副本集在不同的端口上运行,并使用--dbpath
设置指定数据目录的路径。If you are already using the suggested ports, select different ports.如果您已经在使用建议的端口,请选择其他端口。
The instances bind to both the localhost and the ip address of the host.实例绑定到本地主机和主机的ip地址。
The --oplogSize
setting reduces the disk space that each mongod
instance uses. --oplogSize
设置减少了每个mongod
实例使用的磁盘空间。[1] This is ideal for testing and development deployments as it prevents overloading your machine. 这是测试和开发部署的理想选择,因为它可以防止机器过载。For more information on this and other configuration options, see Configuration File Options.有关此配置选项和其他配置选项的详细信息,请参阅配置文件选项。
Connect to one of your 通过mongod
instances through mongosh
. mongosh
连接到您的一个mongod
实例。You will need to indicate which instance by specifying its port number. 您需要通过指定其端口号来指示哪个实例。For the sake of simplicity and clarity, you may want to choose the first one, as in the following command;为了简单明了,您可能需要选择第一个,如下命令所示;
mongosh --port 27017
In 在mongosh
, use rs.initiate()
to initiate the replica set. mongosh
中,使用rs.initiate()
启动副本集。You can create a replica set configuration object in 您可以在mongosh
environment, as in the following example:mongosh
环境中创建副本集配置对象,如以下示例所示:
rsconf = { _id: "rs0", members: [ { _id: 0, host: "<hostname>:27017" }, { _id: 1, host: "<hostname>:27018" }, { _id: 2, host: "<hostname>:27019" } ] }
replacing 将<hostname>
with your system's hostname, and then pass the rsconf
file to rs.initiate()
as follows:<hostname>
替换为系统的主机名,然后将rsconf
文件传递给rs.initiate()
,如下所示:
rs.initiate( rsconf )
Display the current replica configuration by issuing the following command:通过发出以下命令显示当前副本配置:
rs.conf()
The replica set configuration object resembles the following:副本集配置对象类似于以下内容:
{ "_id" : "rs0", "version" : 1, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "<hostname>:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "secondaryDelaySecs" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "<hostname>:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "secondaryDelaySecs" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "<hostname>:27019", "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("598f630adc9053c6ee6d5f38") } }
Check the status of your replica set at any time with the 随时使用rs.status()
operation.rs.status()
操作检查副本集的状态。
The documentation of the following shell functions for more information:有关详细信息,请参阅以下shell函数的文档:
You may also consider the simple setup script as an example of a basic automatically-configured replica set.您还可以将简单设置脚本作为基本自动配置副本集的示例。
Refer to Replica Set Read and Write Semantics for a detailed explanation of read and write semantics in MongoDB.有关MongoDB中读写语义的详细说明,请参阅副本集读写语义。
[1] | majority commit point . |