对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

replSetInitiate(数据库命令)

replSetInitiate

The replSetInitiate command initializes a new replica set.

提示

In mongosh, this command can also be run through the rs.initiate() helper method.

Helper methods are convenient for mongosh users, 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.

要运行 replSetInitiate,请使用 db.runCommand( { <command> } ) 方法。

注意

仅在副本集的其中一个 mongod 实例上运行命令。

此命令可用于以下环境中托管的部署:

重要

MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

该命令具有以下语法:

db.runCommand(
{
replSetInitiate : <config_document>
}
)

<config_document> 是指定副本集配置的文档。例如,下面是一个用于创建简单 3-member 副本集的配置文档:

{
_id : <setname>,
members : [
{_id : 0, host : <host0>},
{_id : 1, host : <host1>},
{_id : 2, host : <host2>},
]
}

重要

要避免因 IP 地址变更而更新配置,请使用 DNS 主机名而非 IP 地址。在配置副本集成员或分片集群成员时,使用 DNS 主机名而非 IP 地址尤为重要。

在水平分割网络配置下,请使用主机名而非 IP 地址来配置集群。从 MongoDB 5.0 开始,仅配置了 IP 地址的节点将无法通过启动验证,因而不会启动。

警告

将实例绑定到可公开访问的 IP 地址之前,必须保护集群免遭未经授权的访问。有关安全建议的完整列表,请参阅自管理部署的安全清单。至少应考虑启用身份验证强化网络基础设施。

MongoDB 二进制文件 mongodmongos 默认绑定到本地主机。如果为此二进制文件设置了 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. 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.

要覆盖默认绑定并绑定到其他 IP 地址,请使用 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. See disableSplitHorizonIPCheck.

例如,以下 mongod 实例会绑定到本地主机和主机名 My-Example-Associated-Hostname,而该主机名与 IP 地址 198.51.100.1 相关联:

mongod --bind_ip localhost,My-Example-Associated-Hostname

为了连接到此实例,远程客户端必须指定主机名或其关联的 IP 地址 198.51.100.1

mongosh --host My-Example-Associated-Hostname
mongosh --host 198.51.100.1

将配置文档分配给变量,然后将该文档传递给 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)

重要

要避免因 IP 地址变更而更新配置,请使用 DNS 主机名而非 IP 地址。在配置副本集成员或分片集群成员时,使用 DNS 主机名而非 IP 地址尤为重要。

在水平分割网络配置下,请使用主机名而非 IP 地址来配置集群。从 MongoDB 5.0 开始,仅配置了 IP 地址的节点将无法通过启动验证,因而不会启动。

请注意,省略端口会导致主机使用默认端口 27017。另请注意,您可以在配置文档中指定其他选项,例如本示例中的 arbiterOnly 设置。

给本页内容打分