Definition定义
replSetInitiateThereplSetInitiatecommand initializes a new replica set.replSetInitiate命令初始化新的副本集。Tip
In在mongosh, this command can also be run through thers.initiate()helper method.mongosh中,此命令也可以通过rs.initiate()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用数据库命令。To run要运行replSetInitiate, use thedb.runCommand( { <command> } )method.replSetInitiate,请使用db.runCommand( { <command> } )方法。Note
Run the command on only one of the仅在副本集的一个mongodinstances for the replica set.mongod实例上运行该命令。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Important
This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.runCommand(
{
replSetInitiate : <config_document>
}
)
The <config_document> is a document that specifies the replica set's configuration. For instance, here's a config document for creating a simple 3-member replica set:<config.docu>是一个指定副本集配置的文档。例如,这里有一个配置文档,用于创建一个简单的3成员副本集:
{
_id : <setname>,
members : [
{_id : 0, host : <host0>},
{_id : 1, host : <host1>},
{_id : 2, host : <host2>},
]
}
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用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 fail startup validation and do not start.使用主机名而不是IP地址来配置跨拆分网络范围的集群。从MongoDB 5.0开始,仅配置了IP地址的节点无法启动验证,也无法启动。
IP BindingIP绑定
Warning
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 for Self-Managed Deployments. 有关安全建议的完整列表,请参阅自我管理部署的安全检查表。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地址列表。
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. See 从MongDB 5.0开始,仅配置了IP地址的拆分水平DNS节点无法通过启动验证并报告错误。请参阅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.1Example示例
Assign a config document to a variable and then to pass the document to the 将配置文档分配给变量,然后将文档传递给rs.initiate() helper:rs.initiate()帮助程序:
config = {
_id : "my_replica_set",
members : [
{_id : 0, host : "rs1.example.net:27017"},
{_id : 1, host : "rs2.example.net:27017"},
{_id : 2, host : "rs3.example.net", arbiterOnly: true},
]
}
rs.initiate(config)
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.为避免因IP地址更改而进行配置更新,请使用DNS主机名而不是IP地址。在配置副本集成员或分片集群成员时,使用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 fail startup validation and do not start.使用主机名而不是IP地址来配置跨拆分网络范围的集群。从MongoDB 5.0开始,仅配置了IP地址的节点无法启动验证,也无法启动。
Notice that omitting the port cause the host to use the default port of 27017. Notice also that you can specify other options in the config documents such as the 请注意,省略端口会导致主机使用默认端口27017。还请注意,您可以在配置文档中指定其他选项,例如本例中的arbiterOnly设置。arbiterOnly setting in this example.