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

在副本集上创建滚动索引构建

滚动索引构建是默认索引构建的替代方案。

仅当您的部署符合以下情况之一时,才使用滚动索引构建:

  • 如果平均 CPU 利用率超过 (N-1)/N-10%,其中 N 是mongod可用的 CPU 线程数

  • 如果您的WiredTiger缓存填充率经常超过 90%

警告

避免同时执行滚动索引和复制索引构建过程,因为这可能会导致意外问题,例如构建中断和循环崩溃。

提示

借助Atlas,您可以临时扩展集群以满足传统索引构建的要求。但是, Atlas会对集群的扩展收取费用。有关更多信息,请参阅集群配置成本。

重要

Atlas Infinite 集群不支持此功能。

要通过以下过程创建唯一索引,必须在此过程中停止对集合的所有写入。

如果您在执行步骤的过程中无法停止所有对集合的写入操作,请不要使用本页所列的步骤。作为替代方案,请在副本集的主节点上使用 db.collection.createIndex() 来为集合构建唯一索引。

确保您的 oplog 足够大,以允许索引或重新索引操作完成,而不会落后太远而无法赶上。有关更多信息,请参阅 oplog 大小调整文档。

滚动索引构建会降低集群的弹性并增加构建持续时间。

用于构建唯一索引

要通过以下过程创建唯一索引,必须在索引构建期间停止对集合的所有写入。否则,副本集节点之间可能会获得不一致的数据。

警告

如果无法停止对集合的所有写入,请勿使用以下步骤创建唯一索引。

重要

以滚动方式构建索引的以下过程适用于副本集部署,而不是分片的集群。有关针对分片的集群的过程,请参阅在分片集群上创建滚动索引构建。

在主节点 (primary node in the replica set)节点上运行以下命令,隐藏将构建新索引的从节点(secondary node from replica set)。

在此示例中,构建新索引的从节点(secondary node from replica set)是 cfg.members 中的第三个节点。

var cfg = rs.conf();
// Record originalPriority so that you can reset it later.
var originalPriority = cfg.members[2].priority;
cfg.members[2].priority = 0;
cfg.members[2].hidden = 1;
rs.reconfig(cfg);

停止与从节点关联的 mongod 进程。执行以下配置更新后重新启动:

如果使用配置文件,请进行以下配置更新:

  • 注释掉 replication.replSetName 选项。

  • Change the net.port to a different port. [1] Make a note of the original port setting as a comment.

  • 在 setParameter 部分中将参数 disableLogicalSessionCacheRefresh 设置为 true。

例如,为副本集节点更新的配置文件将包含类似于如下示例的内容:

net:
bindIp: localhost,<hostname(s)|ip address(es)>
port: 27217
# port: 27017
#replication:
# replSetName: myRepl
setParameter:
disableLogicalSessionCacheRefresh: true

其他设置(例如storage.dbPath 等等)保持不变。

并重新启动:

mongod --config <path/To/ConfigFile>
[1](1, 2) By running the mongod on a different port, you ensure that the other members of the replica set and all clients will not contact the member while you are building the index.

直接连接到在新端口上独立运行的 mongod 实例,并为该实例创建新索引。

For example, connect mongosh to the instance, and use the createIndex() to create an ascending index on the username field of the records collection:

db.records.createIndex( { username: 1 } )

索引构建完成后,关闭mongod 实例。要将节点返回到其原始配置,请撤消您在以独立运行启动该节点时所做的配置更改。 然后,将该节点作为副本集的成员重新启动。

重要

请务必删除 disableLogicalSessionCacheRefresh 参数。

例如,要重新启动副本集节点:

如果您在使用配置文件:

例如:

net:
bindIp: localhost,<hostname(s)|ip address(es)>
port: 27017
replication:
replSetName: myRepl

其他设置(例如storage.dbPath 等等)保持不变。

并重新启动:

mongod --config <path/To/ConfigFile>

重要

在开始下一步之前,允许复制赶上该成员。

在主节点 (primary node in the replica set)上运行以下命令,取消隐藏构建索引的从节点(secondary node from replica set)节点。 本示例中,构建索引的从节点(secondary node from replica set)节点是 cfg.members 中的第三个节点。

var cfg = rs.conf();
cfg.members[2].priority = originalPriority;
cfg.members[2].hidden = false;
rs.reconfig(cfg);

一旦该节点赶上副本集中的其他节点,请一次对一个剩余从节点重复该过程:

当所有从节点均有新索引时,退出主节点,使用上述过程将其重启为独立实例,然后构建前一个主节点的索引:

  1. Use the rs.stepDown() method in mongosh to step down the primary. Upon successful stepdown, the current primary becomes a secondary and the replica set members elect a new primary.