Rolling Index Builds on Replica Sets基于副本集的滚动索引构建

On this page本页内容

Index builds can impact replica set performance. 索引生成可能会影响副本集性能。By default, MongoDB 4.4 and later build indexes simultaneously on all data-bearing replica set members. 默认情况下,MongoDB 4.4和更高版本在所有承载数据的副本集成员上同时构建索引。For workloads which cannot tolerate performance decrease due to index builds, consider using the following procedure to build indexes in a rolling fashion.对于不能容忍由于索引生成而导致性能下降的工作负载,请考虑使用以下过程以滚动方式生成索引。

Rolling index builds take at most one replica set member out at a time, starting with the secondary members, and builds the index on that member as a standalone. 滚动索引生成一次最多提取一个副本集成员,从辅助成员开始,并将该成员作为独立成员生成索引。Rolling index builds require at least one replica set election.滚动索引生成需要至少选择一个副本集。

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.相反,通过在副本集的主索引上发出db.collection.createIndex()来在集合上构建唯一索引。

Oplog SizeOplog大小

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. 确保oplog足够大,以允许索引或重新索引操作完成,而不会落后太多而无法赶上。See the oplog sizing documentation for additional information.有关更多信息,请参阅oplog大小调整文档。

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 Rolling Index Builds on Sharded Clusters instead.有关分片群集的过程,请参阅基于分片群集的滚动索引构建

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

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

If you are using a configuration file, make the following configuration updates:

  • Comment out the replication.replSetName option.

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

  • Set parameter disableLogicalSessionCacheRefresh to true in the setParameter section.

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.

And restart:

mongod --config <path/To/ConfigFile>

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:

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

B. 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 } )

C. Restart the Program mongod as a Replica Set Member作为副本集成员重新启动程序mongod

When the index build completes, shutdown the mongod instance. 索引构建完成后,关闭mongod实例。Undo the configuration changes made when starting as a standalone to return the its original configuration and restart as a member of the replica set.撤消作为独立启动时所做的配置更改,以恢复其原始配置,并作为副本集的成员重新启动。

Important重要

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

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

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.

And restart:

mongod --config <path/To/ConfigFile>

If you are using command-line options,

  • Revert to the original port number

  • Include the --replSet option.

  • Remove parameter disableLogicalSessionCacheRefresh.

For example:

mongod --port 27017 --replSet myRepl

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

Allow replication to catch up on this member.允许复制赶上此成员。

D. Repeat the Procedure for the Remaining Secondaries对其余的辅助设备重复此步骤

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:一旦该成员赶上了集合中的其他成员,则对其余次要成员一次重复一个成员的过程:

  1. A. Stop One Secondary and Restart as a Standalone停止一个辅助设备并作为独立设备重新启动
  2. B. Build the Index建立索引
  3. C. Restart the Program mongod as a Replica Set Member作为副本集成员重新启动程序mongod

E. Build the Index on the 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. A. Stop One Secondary and Restart as a Standalone停止一个辅助设备并作为独立设备重新启动
  3. B. Build the Index建立索引
  4. C. Restart the Program mongod as a Replica Set Member作为副本集成员重新启动程序mongod
←  Index Builds on Populated CollectionsRolling Index Builds on Sharded Clusters →