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.可选地,该方法可以采用保存副本集配置的文档形式的参数。

The rs.initiate() method has the following parameter:rs.initiate()方法具有以下参数:

Parameter参数Type类型Description描述
configurationdocumentOptional. 可选。A document that specifies configuration for the new replica set. 指定新副本集配置的文档。If a configuration is not specified, MongoDB uses a default replica set configuration. 如果未指定配置,MongoDB将使用默认副本集配置。

The rs.initiate() method provides a wrapper around the replSetInitiate command.rs.initiate()方法提供replSetInitiate命令的包装。

IP BindingIP绑定

Warning警告

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, mongod and mongos, bind to localhost by default. MongoDB二进制文件mongodmongos默认绑定到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命令行选项,则该二进制文件还将绑定到本地主机ipv6地址。

By default mongod and mongos that are bound to localhost only accept connections from clients that are running on the same computer. 默认情况下,绑定到localhost的mongodmongos只接受来自同一台计算机上运行的客户端的连接。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 net.bindIp configuration file setting or the --bind_ip command-line option to specify a list of hostnames or IP addresses.要覆盖默认绑定并绑定到其他IP地址,请使用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. 从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 198.51.100.1:为了连接到此实例,远程客户端必须指定主机名或其关联的IP地址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.有关副本集配置文档的详细信息,请参阅副本集配置文件示例

Important重要

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地址的节点将无法启动验证,无法启动。

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实例必须以设置为myReplSet--replSet命令行选项(如果使用配置文件,则为replication.replSetName)和适当设置的--bind_ip(如果使用了配置文件,或为net.bindIp)开始,以便副本集的其他成员和客户端可以连接。

Warning警告

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.至少要考虑启用身份验证加强网络基础设施

Connect mongosh to one of the mongod instances and run rs.initiate().mongosh连接到其中一个mongod实例并运行rs.initiate()

Note注意

Run rs.initiate() on just one and only onemongod 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. 为了避免由于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地址的节点将无法启动验证,无法启动。

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.有关部署副本集的详细信息,请参阅部署副本集

←  rs.help()rs.printReplicationInfo() →