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.否则,您可能会在副本集成员之间得到不一致的数据。
If you cannot stop all writes to the collection, do not use the following procedure to create unique indexes.如果无法停止对集合的所有写入,请不要使用以下过程创建唯一索引。
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:
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:
Remove --replSet
.
Set parameter disableLogicalSessionCacheRefresh
to true
in the --setParameter
option.
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) 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 例如,将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 } )
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.撤消作为独立启动时所做的配置更改,以恢复其原始配置,并作为副本集的成员重新启动。
Be sure to remove the 确保删除disableLogicalSessionCacheRefresh
parameter.disableLogicalSessionCacheRefresh
参数。
For example, to restart your replica set member:例如,要重新启动副本集成员:
If you are using a configuration file:
Revert to the original port number.
Uncomment the replication.replSetName
.
Remove parameter disableLogicalSessionCacheRefresh
in the setParameter
section.
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>
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 mongosh
to step down the primary. mongosh
中使用rs.stepDown()
方法来逐步降低主。mongod
as a Replica Set Membermongod