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:如果使用的是配置文件,请进行以下配置更新:

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>

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

  • Remove 去除--replSet.
  • Modify --port to a different port. --port更改为其他端口。[1]
  • Set parameter disableLogicalSessionCacheRefresh to true in the --setParameter option.--setParameter选项中将参数disableLogicalSessionCacheRefresh设置为true

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.其他设置(例如--dbpath等)保持不变。

[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 a mongo shell to the instance, and use the createIndex() to create an ascending index on the username field of the records collection:例如,将mongo shell连接到实例,并使用createIndex()在记录集合的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.其他设置(如storage.dbPath等)保持不变。

And restart:然后重新启动:

mongod --config <path/To/ConfigFile>

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等)保持不变。

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 the mongo shell to step down the primary. 使用mongo shell中的rsstepDown()方法来降低主进程。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