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 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. 确保您的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.若要使用以下过程创建唯一索引,必须在索引生成过程中停止对集合的所有写入操作。否则,复制副本集成员之间的数据可能会不一致。WarningIf you cannot stop all writes to the collection, do not use the following procedure to create unique indexes.如果无法停止对集合的所有写入,请不要使用以下过程创建唯一索引。
Procedure过程
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. Restart after making the following configuration updates:mongod
进程。进行以下配置更新后重新启动:
If you are using a configuration file, make the following configuration updates:如果您正在使用配置文件,请进行以下配置更新:
-
Comment out the注释掉replication.replSetName
option.replication.replSetName
选项。 -
Change the将net.port
to a different port.net.port
更改为其他端口。[1]Make a note of the original port setting as a comment.记下原始端口设置作为注释。 -
Set parameter在disableLogicalSessionCacheRefresh
totrue
in thesetParameter
section.setParameter
部分中将参数disableLogicalSessionCacheRefresh
设置为true
。
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
.--replSet
。 -
Set parameter在disableLogicalSessionCacheRefresh
totrue
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
:
[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 ,可以确保副本集的其他成员和所有客户端在构建索引时不会与该成员联系。 |
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
mongod
as a Replica Set MemberWhen 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
.replication.replSetName
。 -
Remove parameter删除disableLogicalSessionCacheRefresh
in thesetParameter
section.setParameter
部分中的参数disableLogicalSessionCacheRefresh
。
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恢复到原始端口号 -
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:一旦该成员赶上集合中的其他成员,则对其余次要成员重复该过程,每次一个成员:
E. 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:当所有辅助设备都有新索引时,请关闭主索引,使用上述过程将其作为独立索引重新启动,并在前一个主索引上构建索引:
Use the使用rs.stepDown()
method inmongosh
to step down the primary.mongosh
中的rs.stepDown()
方法来逐步减少primary
。Upon successful stepdown, the current primary becomes a secondary and the replica set members elect a new primary.成功降级后,当前主副本将变为辅助副本,副本集成员将选择一个新的主副本。- A.
Stop One Secondary and Restart as a Standalone停止一个辅助设备并作为单机版重新启动 - B.
Build the Index建立索引 - C.
Restart the Program以副本集成员身份重新启动程序mongod
as a Replica Set Membermongod