副本集上的滚动索引构建(逐个成员)
MongoDB维护命令
// 1) Shut down a SECONDARY; comment out replication.replSetName and start it standalone on another port:
// mongod --config /etc/mongod.conf --port 27217 --setParameter disableLogicalSessionCacheRefresh=true
// 2) Build the index on the standalone member:
db.getSiblingDB("shop").musteri.createIndex({ email: 1 }, { name: "idx_email" })
// 3) Restart with the normal configuration; wait until it is SECONDARY and lag has closed:
rs.printSecondaryReplicationInfo()
// 4) When all secondaries are done, step down the primary and apply steps 1-3 to the old primary:
rs.stepDown(120, 30)作用
通过临时让成员脱离副本集以 standalone 方式运行,在每个成员上分别构建索引;primary 从不承担构建负载。
适用场景
对于超大集合,默认分布式构建对 primary 和复制延迟的影响不可接受时。
注意
每个成员的维护时间必须短于 oplog 窗口;否则成员回归时已经 too stale。以滚动方式构建唯一索引时,在整个构建期间停止对该集合的写入;否则某些成员上可能出现违反约束的文档。在不同端口上启动 standalone 成员,以免应用连接到它。