rs.initiate()
On this page本页内容
Description描述
rs.initiate(configuration)
-
Initiates a replica set.启动复制副本集。Optionally, the method can take an argument in the form of a document that holds the configuration of a replica set.可选地,该方法可以采用文档形式的参数,该文档包含副本集的配置。Importantmongosh Method
This page documents a
mongosh
method. This is not the documentation for database commands or language-specific drivers, such as Node.js.For the database command, see the有关数据库命令,请参阅replSetInitiate
command.replSetInitiate
命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。For the legacy对于遗留的mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:mongo
shell文档,请参阅相应MongoDB Server版本的文档:Thers.initiate()
method has the following parameter:rs.initiate()
方法具有以下参数:Parameter参数Type类型Description描述configuration
document Optional.可选的。A document that specifies configuration for the new replica set.指定新复制副本集配置的文档。If a configuration is not specified, MongoDB uses a default replica set configuration.如果没有指定配置,MongoDB将使用默认的副本集配置。
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. mongosh
和复制集或分片集群的其他成员。Remote clients cannot connect to binaries that are bound only to localhost.远程客户端无法连接到仅绑定到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
Replica Set Configuration副本集配置
See Replica Set Configuration Document Example for details of the replica set configuration document.有关副本集配置文档的详细信息,请参阅副本集配置文件示例。
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地址的节点将无法通过启动验证,也不会启动。
Example实例
The following example initiates a new replica set with three members.以下示例启动一个包含三个成员的新复制副本集。
The three 三个mongod
instances must have started with the --replSet
command line option (or replication.replSetName
if using a configuration file) set to myReplSet
and the --bind_ip
(or net.bindIp
if using a configuration file) set appropriately such that other members of the replica set and clients can connect.mongod
实例必须在启动时将--replSet
命令行选项(如果使用配置文件,则为replication.replSetName
)设置为myReplSet
,并适当设置--bind_ip
(如果使用了配置文件,请为net.bindIp
),以便副本集的其他成员和客户端可以连接。
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.至少,考虑启用身份验证和强化网络基础设施。
Connect 将mongosh
to one of the mongod
instances and run rs.initiate()
.mongosh
连接到其中一个mongod
实例,然后运行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. 使用主机名而不是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地址的节点将无法通过启动验证,也不会启动。
rs.initiate(
{
_id: "myReplSet",
version: 1,
members: [
{ _id: 0, host : "mongodb0.example.net:27017" },
{ _id: 1, host : "mongodb1.example.net:27017" },
{ _id: 2, host : "mongodb2.example.net:27017" }
]
}
)
For details on replica set configuration, see Replica Set Configuration Fields.有关副本集配置的详细信息,请参阅副本集配置字段。
For details on deploying a replica set, see Deploy a Replica Set.有关部署副本集的详细信息,请参阅部署副本集。