在副本集上构建索引

在本页面

为了最大程度地减少在 副本集部署 上构建索引的影响,请使用以下过程以滚动方式构建索引。有关在分片群集上构建索引的方法,请参见在分片群集上构建索引

以下用于副本集部署的过程确实一次从副本集中删除一个成员。但是,此过程一次只会影响集合中的一个成员,而不会同时影响“所有”次要成员。

Considerations

Unique Indexes

要使用以下过程创建unique indexes,您必须在此过程中停止对集合的所有写操作。

如果在此过程中无法停止对集合的所有写操作,请不要使用此页面上的过程。相反,通过在副本集的主数据库上发出db.collection.createIndex()在集合上构建唯一索引。

Oplog Size

确保oplog足够大,以允许完成索引编制或重新索引编制操作,而又不会落后太多。有关其他信息,请参见oplog sizing文档。

Prerequisites

  • 用于构建唯一索引

    • 要使用以下过程创建unique indexes,必须在索引构建过程中停止对集合的所有写操作。否则,最终可能导致整个副本集成员的数据不一致。

Warning

如果无法停止对集合的所有写操作,请不要使用以下过程创建唯一索引。

Procedure

Important

以下以滚动方式构建索引的过程适用于副本集部署,而不适用于分片群集。有关具有副本集分片的分片群集的过程,请参阅在分片群集上构建索引

A.停止一台辅助服务器并以独立方式重新启动

停止与辅助服务器关联的mongod进程。进行以下配置更新后,重新启动:

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.

  • 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:

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]*(12)*通过在其他端口上运行mongod,可以确保副本集的其他成员和所有 Client 端在构建索引时不会与该成员联系。

B.构建索引

直接连接到在新端口上独立运行的mongod实例,并为此实例创建新索引。

例如,将mongoShell 连接到实例,然后使用createIndex()records集合的username字段上创建升序索引:

db.records.createIndex( { username: 1 } )

C.以副本集成员身份重新启动 Program mongod

索引构建完成后,关闭mongod实例。撤消以独立版本启动时所做的配置更改,以返回其原始配置并以副本集的成员身份重新启动。

Important

确保删除disableLogicalSessionCacheRefresh参数。

例如,重新启动副本集成员:

Configuration File
Command-line Options

If you are using a configuration file:

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>

If you are using command-line options,

  • Revert to the original port number

  • Include the --replSet option.

  • Remove parameter disableLogicalSessionCacheRefresh .

For example:

mongod --port 27017 --replSet myRepl

Other settings (e.g. --dbpath, etc.) remain the same.

允许复制赶上该成员。

D.重复其余中学的程序

一旦该成员赶上了集合中的其他成员,请对其余的次要成员一次重复一个成员的过程:

E.在主数据库上构建索引

当所有辅助节点都具有新索引时,请降级主节点,然后使用上述过程将其作为独立实例重新启动,然后在先前的主节点上构建索引: