- Indexes >
- Index Build Operations on a Populated Collection >
- Build Indexes on Replica Sets
Build Indexes on Replica Sets¶
On this page
To minimize the impact of building an index on replica sets and sharded clusters with replica set shards, use the following procedure to build indexes in a rolling fashion.
This procedure does take one member out of the replica set at a time. However, this procedure will only affect one member of the set at a time rather than all secondaries at the same time.
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, or - issuing
db.collection.createIndex()
on themongos
for a sharded cluster.
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. See the oplog sizing documentation for additional information.
Procedure¶
Note
If you need to build an index in a sharded cluster, repeat the following procedure for each replica set that provides each shard.
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.
Stop One Secondary and Restart as a Standalone¶
Stop the mongod
process on one secondary. Restart the
mongod
process, making the following configuration updates:
- Configuration File
- Command-line Options
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. - If the
mongod
is a shard or config server member, you must also:- Comment out the
sharding.clusterRole
option. - Set parameter
skipShardingConfigurationChecks
(available in MongoDB 3.4.11+) totrue
in thesetParameter
section.
- Comment out the
For example, for a shard/config server replica set member, the updated configuration file will include content like the following example:
If using command-line options, make the following configuration updates:
- Remove
--replSetName
. - Modify
--port
to a different port. [1] - If the
mongod
is a shard or config server member, you must also:- Remove
--shardsvr
if a shard member and--configsvr
if a config server member. - Set parameter
skipShardingConfigurationChecks
(available in MongoDB 3.4.11+) totrue
in thesetParameter
section.
- Remove
For example, if your replica set member that is not part of
a sharded cluster normally runs with on the default port
of 27017
with the --replSet
option you would use the following invocation:
[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. |
Build the Index¶
Create the new index using the createIndex()
in the mongo
shell, or comparable method in your
driver. This operation will create the index on this
mongod
instance
For example, to create an ascending index on the username
field of
the records
collection, use the following mongo
shell
operation:
Restart the Program mongod
as a Replica Set Member¶
When the index build completes, restart the mongod
instance as a member of the replica set using its normal configuration
file or command-line arguments. That is, undo the configuration changes
made when starting as a standalone. For shard or config server replica
set members, be sure to remove the
skipShardingConfigurationChecks
parameter.
For example:
Modify the port number (e.g. 27017
) as needed. Include any other
options as appropriate for this member.
Allow replication to catch up on this member.
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:
Build the Index on the Primary¶
You can build the index on the primary once you have completed building the index on the secondaries. You can either:
- Build the index in the background on the primary.
- Use the
rs.stepDown()
method in themongo
shell to step down the primary. Upon successful stepdown, the current primary becomes a secondary and the replica set members elect a new primary. - Stop One Secondary and Restart as a Standalone
- Build the Index
- Restart the Program mongod as a Replica Set Member