On this page本页内容
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()
方法具有以下参数:
configuration | document |
The rs.initiate()
method provides a wrapper around the replSetInitiate
command.rs.initiate()
方法提供replSetInitiate
命令的包装。
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
默认绑定到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 默认情况下,绑定到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 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
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 MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅配置IP地址的节点将无法启动验证,无法启动。
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
)开始,以便副本集的其他成员和客户端可以连接。
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()
。
Run 仅在副本集的一个且仅一个rs.initiate()
on just one and only onemongod
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 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.有关部署副本集的详细信息,请参阅部署副本集。