Database Manual / Indexes / Builds / Rolling Index Builds

Create a Rolling Index Build on Replica Sets在副本集上创建滚动索引构建

About this Task关于此任务

Rolling index builds are an alternative to default index builds.滚动索引构建是默认索引构建的替代方案。

Only use a rolling index build if your deployment matches one of the following cases:仅当部署与以下情况之一匹配时,才使用滚动索引构建

  • If your average CPU utilization exceeds (N-1)/N-10% where where N is the number of CPU threads available to mongod如果平均CPU利用率超过(N-1)/N-10%,其中N是mongod可用的CPU线程数
  • If your WiredTiger cache fill ratio regularly exceeds 90%如果WiredTiger缓存填充率经常超过90%

Warning

Avoid performing rolling index and replicated index build processes concurrently as it might lead to unexpected issues, such as broken builds and crash loops.避免同时执行滚动索引和复制索引构建过程,因为这可能会导致意外问题,如构建中断和崩溃循环。

Note

If your deployment does not meet this criteria, use the default index build.如果部署不符合此条件,请使用默认索引构建

Tip

With Atlas, you can temporarily scale your cluster to meet the requirements for a traditional index build. However, Atlas charges to scale your cluster. See Cluster Configuration Costs for more information.使用Atlas,您可以临时扩展集群以满足传统索引构建的要求。但是,Atlas会收取扩展集群的费用。有关更多信息,请参阅群集配置成本。

Considerations注意事项

Unique Indexes唯一索引

To create unique indexes using the following procedure, you must stop all writes to the collection during this procedure.要使用以下过程创建唯一索引,您必须在此过程中停止对集合的所有写入。

If you cannot stop all writes to the collection during this procedure, do not use the procedure on this page. Instead, build your unique index on the collection by issuing db.collection.createIndex() on the primary for a replica set.如果在此过程中无法停止对集合的所有写入,请不要使用此页面上的过程。相反,通过在副本集的Primary节点上发出db.collection.createIndex(),在集合上构建唯一索引。

Oplog Size操作日志大小

Ensure that your oplog is large enough to permit the indexing or re-indexing operation to complete without falling too far behind to catch up. See the oplog sizing documentation for additional information.确保oplog足够大,以允许索引或重新索引操作完成,而不会落后太多而无法赶上。有关更多信息,请参阅oplog大小文档。

Rolling index builds lower the resiliency of your cluster and increase build duration.滚动索引构建会降低集群的弹性,并增加构建持续时间。

Prerequisites先决条件

For building unique indexes用于构建独特的索引

To create unique indexes using the following procedure, you must stop all writes to the collection during the index build. Otherwise, you may end up with inconsistent data across the replica set members.要使用以下过程创建唯一索引,您必须在索引构建期间停止对集合的所有写入。否则,您可能会在副本集成员之间得到不一致的数据。

Warning

If you cannot stop all writes to the collection, do not use the following procedure to create unique indexes.如果无法停止对集合的所有写入,请不要使用以下过程创建唯一索引。

Procedure过程

Important

The following procedure to build indexes in a rolling fashion applies to replica set deployments, and not sharded clusters. For the procedure for sharded clusters, see Create Rolling Index Builds on Sharded Clusters instead.以下以滚动方式构建索引的过程适用于副本集部署,而不是分片集群。有关分片群集的过程,请参阅在分片群集上创建滚动索引构建

1. Hide and Restart One Secondary.隐藏并重新启动一个Secondary。

Run the following commands on your primary node to hide the secondary that will build the new index.在primary节点上运行以下命令以隐藏将构建新索引的secondary节点。

In this example, the secondary that will build the new index is the third node in cfg.members.在这个例子中,将构建新索引的Secondary节点是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);

2. Stop One Secondary and Restart as a Standalone.停止一个Secondary并作为独立设备重新启动。

Stop the mongod process associated with a secondary. Restart after making the following configuration updates:停止与Secondary关联的mongod进程。进行以下配置更新后重新启动:

Configuration File配置文件

If you are using a configuration file, make the following configuration updates:如果您正在使用配置文件,请进行以下配置更新:

For example, the updated configuration file for a replica set member will include content like the following example:例如,副本集成员的更新配置文件将包括以下示例中的内容:

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

Other settings (e.g. storage.dbPath, etc.) remain the same.其他设置(如storage.dbPath等)保持不变。

And restart:然后重新启动:

mongod --config <path/To/ConfigFile>
Command-line Options命令行选项

If using command-line options, make the following configuration updates:如果使用命令行选项,请进行以下配置更新:

For example, if your replica set member normally runs with on the default port of 27017 and the --replSet option, you would specify a different port, omit the --replSet option, and set disableLogicalSessionCacheRefresh parameter to true:例如,如果副本集成员通常在默认端口27017--replSet选项上运行,您可以指定一个不同的端口,省略--replSet参数,并将disableLogicalSessionCacheRefresh参数设置为true

mongod --port 27217 --setParameter disableLogicalSessionCacheRefresh=true

Other settings (e.g. --dbpath, etc.) remain the same.

[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,您可以确保副本集的其他成员和所有客户端在构建索引时不会联系到该成员。

3. Build the Index.构建索引。

Connect directly to the mongod instance running as a standalone on the new port and create the new index for this instance.直接连接到在新端口上作为独立运行的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:例如,将mongosh连接到实例,并使用createIndex()records集合的username字段上创建一个升序索引:

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

4. Restart the Program mongod as a Replica Set Member.以副本集成员的身份重新启动程序mongod

When the index build completes, shutdown the mongod instance. To return the node to its original configuration, undo the configuration changes that you made when you started the node as a standalone. Then, restart the node as a member of the replica set.索引构建完成后,关闭mongod实例。要将节点恢复到其原始配置,请撤消您在将节点作为独立节点启动时所做的配置更改。然后,以副本集成员的身份重新启动节点。

Important

Be sure to remove the disableLogicalSessionCacheRefresh parameter.请确保删除disableLogicalSessionCacheRefresh参数。

For example, to restart your replica set member:例如,要重新启动副本集成员:

Configuration File配置文件

If you are using a configuration file:如果您使用的是配置文件:

For example:例如:

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

Other settings (e.g. storage.dbPath, etc.) remain the same.其他设置(如storage.dbPath等)保持不变。

And restart:然后重新启动:

mongod --config <path/To/ConfigFile>
Command-line Options命令行选项

If you are using command-line options,如果您正在使用命令行选项,

  • Revert to the original port number恢复到原始端口号
  • Include the --replSet option.包括--replSet选项。
  • Remove parameter disableLogicalSessionCacheRefresh.删除参数disableLogicalSessionCacheRefresh

For example:例如:

mongod --port 27017 --replSet myRepl

Other settings (e.g. --dbpath, etc.) remain the same.其他设置(例如--dbpath等)保持不变。

Important

Allow replication to catch up on this member before you begin the next step.在开始下一步之前,允许复制赶上此成员。

5. Unhide the Secondary.取消隐藏Secondary。

Run the following command on your primary to unhide the secondary node that built the index. In this example, the secondary node that built the index is the third node in cfg.members.在primary节点上运行以下命令,以取消隐藏构建索引的secondary节点。在这个例子中,构建索引的secondary节点是cfg.members中的第三个节点。

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

6. Repeat the Procedure for the Remaining Secondaries.对剩余的Secondary重复该程序。

Once the member catches up with the other members of the set, repeat the procedure one member at a time for the remaining secondary members:一旦成员赶上了集合中的其他成员,对剩余的Secondary成员逐一重复该过程:

  1. Hide and restart one secondary.隐藏并重新启动一个Secondary。
  2. Build the index.构建索引。
  3. Restart the Program mongod as a replica set member.以副本集成员的身份重新启动程序mongod

7. Build the Index on the Primary.在Primary建立索引。

When all the secondaries have the new index, step down the primary, restart it as a standalone using the procedure described above, and build the index on the former primary:当所有次级服务器都有新索引时,请关闭主服务器,使用上述过程将其作为独立服务器重新启动,并在前一个主服务器上构建索引:

  1. Use the rs.stepDown() method in mongosh to step down the primary. 使用mongosh中的rs.stepDown()方法来降低主值。Upon successful stepdown, the current primary becomes a secondary and the replica set members elect a new primary.成功降级后,当前主服务器将成为次服务器,副本集成员将选择一个新的主服务器。
  2. Hide and restart one secondary.隐藏并重新启动一个辅助设备。
  3. Build the index.构建索引。
  4. Restart the Program mongod as a replica set member.以副本集成员的身份重新启动程序mongod