Deploy a Replica Set for Testing and Development部署用于测试和开发的副本集
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 mongod to a Replica Set. 如果您希望从单个MongoDB实例部署副本集,请参阅将独立mongod
转换为副本集。For more information on replica set deployments, see the Replication and Replica Set Deployment Architectures documentation.有关副本集部署的更多信息,请参阅复制和副本集部署体系结构文档。
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.这将确保选举顺利进行。有关设计复制副本集的详细信息,请参阅复制概述。
Requirements要求
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.有关如何检查连接的说明,请参阅测试所有成员之间的连接。
Considerations注意事项
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绑定
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.至少,考虑启用身份验证和强化网络基础设施。
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地址列表。
Starting in MongDB 5.0, split horizon DNS从MongDB 5.0开始,仅配置有IP地址的拆分域DNS nodes that are only configured with an IP address fail startup validation and report an error.
节点无法通过启动验证并报告错误。
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
实例绑定到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.1
In this test deployment, the three members run on the same machine.在这个测试部署中,三个成员在同一台机器上运行。
Replica Set Naming副本集命名
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.如果应用程序连接到多个复制副本集,则每个复制副本集都必须具有不同的名称。某些驱动程序按副本集名称对副本集连接进行分组。
Procedure过程
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 MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongoDB 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通过发出以下命令,在mongod
instances in their own shell windows by issuing the following commands:mongod
实例自己的shell窗口中启动它们:WarningBefore 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.至少,考虑启用身份验证和强化网络基础设施。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. If you are already using the suggested ports, select different ports.rs0
的副本集的成员启动,每个实例都在一个不同的端口上运行,并使用--dbpath
设置指定数据目录的路径。如果您已经在使用建议的端口,请选择不同的端口。The instances bind to both the localhost and the ip address of the host.实例绑定到本地主机和主机的ip地址。The--oplogSize
setting reduces the disk space that eachmongod
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 throughmongosh
.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
, users.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 thersconf
file tors.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()
操作随时检查复制副本集的状态。
See also: 另请参阅:
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 . |