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.滚动索引生成需要至少选择一个副本集。
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()
来在集合上构建唯一索引。
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大小调整文档。
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.如果无法停止对集合的所有写入,请不要使用以下过程创建唯一索引。
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.有关分片群集的过程,请参阅在分片群集上构建滚动索引。
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:如果使用的是配置文件,请进行以下配置更新:
replication.replSetName
option.replication.replSetName
选项。net.port
to a different port. net.port
更改为其他端口。[1] disableLogicalSessionCacheRefresh
to true
in the setParameter
section.setParameter
部分中,将参数disableLogicalSessionCacheRefresh
设置为true
。For example, the updated configuration file for a replica set member will include content like the following example:例如,副本集成员的更新配置文件将包括如下示例所示的内容:
Other settings (e.g. 其他设置(如storage.dbPath
, etc.) remain the same.storage.dbPath
等)保持不变。
And restart:然后重新启动:
If using command-line options, make the following configuration updates:如果使用命令行选项,请进行以下配置更新:
--replSet
.--port
to a different port. --port
更改为其他端口。[1]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
:
Other settings (e.g. 其他设置(例如--dbpath
, etc.) remain the same.--dbpath
等)保持不变。
[1] | (1, 2) 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 ,可以确保副本集的其他成员和所有客户端在构建索引时不会联系该成员。 |
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
字段上创建升序索引:
mongod
as a Replica Set Membermongod
¶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:如果您使用的是配置文件:
replication.replSetName
.replication.replSetName
的注释。disableLogicalSessionCacheRefresh
in the setParameter
section.setParameter
部分中的参数disableLogicalSessionCacheRefresh
。For example:例如:
Other settings (e.g. 其他设置(如storage.dbPath
, etc.) remain the same.storage.dbPath
等)保持不变。
And restart:然后重新启动:
If you are using command-line options,如果使用命令行选项,
--replSet
option.--replSet
选项。disableLogicalSessionCacheRefresh
.disableLogicalSessionCacheRefresh
。For example:例如:
Other settings (e.g. 其他设置(例如--dbpath
, etc.) remain the same.--dbpath
等)保持不变。
Allow replication to catch up on this member.允许复制赶上此成员。
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:一旦该成员赶上集合中的其他成员,对其余的次要成员一次重复一个成员的过程:
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:当所有的辅助设备都有了新的索引后,请退出主设备,使用上述过程将其作为独立设备重新启动,并在前一个主设备上构建索引:
rs.stepDown()
method in the mongo
shell to step down the primary. mongo
shell中的rsstepDown()
方法来降低主进程。mongod