在分片群集上构建索引

在本页面

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

分片群集部署的以下过程确实一次删除了一个分片副本集的成员。但是,此过程一次只会影响一个成员,而不会同时影响所有次要成员。

Considerations

Unique Indexes

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

如果在此过程中无法停止对集合的所有写操作,请不要使用此页面上的过程。而是通过在mongos上为分片群集发出db.collection.createIndex()在集合上构建唯一索引。

Oplog Size

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

Prerequisites

Warning

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

Procedure

A.停止平衡器

mongoShell 连接到分片群集中的mongos实例,然后运行sh.stopBalancer()以禁用平衡器:

sh.stopBalancer()

Note

如果正在进行迁移,则系统将在停止平衡器之前完成正在进行的迁移。

要验证是否禁用了平衡器,请运行sh.getBalancerState(),如果禁用了平衡器,则返回 false:

sh.getBalancerState()

B.确定集合的分布

从连接到mongosmongo shell 中,刷新该mongos的缓存路由表,以避免返回该集合的陈旧分发信息。刷新后,对要构建索引的集合运行db.collection.getShardDistribution()

例如,如果要在test数据库中的records集合上升序索引:

db.adminCommand( { flushRouterConfig: "test.records" } );
db.records.getShardDistribution();

该方法输出碎片分布。例如,考虑一个具有 3 个分片shardAshardBshardC的分片集群,而db.collection.getShardDistribution()返回以下内容:

Shard shardA at shardA/s1-mongo1.example.net:27018,s1-mongo2.example.net:27018,s1-mongo3.example.net:27018
 data : 1KiB docs : 50 chunks : 1
 estimated data per chunk : 1KiB
 estimated docs per chunk : 50

Shard shardC at shardC/s3-mongo1.example.net:27018,s3-mongo2.example.net:27018,s3-mongo3.example.net:27018
 data : 1KiB docs : 50 chunks : 1
 estimated data per chunk : 1KiB
 estimated docs per chunk : 50

Totals
 data : 3KiB docs : 100 chunks : 2
 Shard shardA contains 50% data, 50% docs in cluster, avg obj size on shard : 40B
 Shard shardC contains 50% data, 50% docs in cluster, avg obj size on shard : 40B

从输出中,您仅在shardAshardC上构建test.records的索引。

C.在包含集合块的碎片上构建索引

对于每个包含集合块的分片,请按照以下步骤在分片上构建索引。

C1.停止一台辅助服务器并独立运行

对于受影响的分片,停止与其辅助节点之一关联的mongod进程。进行以下配置更新后,重新启动:

Configuration File
Command-line Options

If you are using a configuration file, make the following configuration updates:

For example, for a shard replica set member, the updated configuration file will include content like the following example:

net:
   bindIp: localhost,<hostname(s)|ip address(es)>
   port: 27218
#   port: 27018
#replication:
#   replSetName: shardA
#sharding:
#   clusterRole: shardsvr
setParameter:
   skipShardingConfigurationChecks: true
   disableLogicalSessionCacheRefresh: true

And restart:

mongod --config <path/To/ConfigFile>

Other settings (e.g. storage.dbPath, etc.) remain the same.

If using command-line options, make the following configuration updates:

For example, restart your shard replica set member without the --replSet and --shardsvr options. Specify a new port number and set both the skipShardingConfigurationChecks and disableLogicalSessionCacheRefresh parameters to true:

mongod --port 27218 --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true

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

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

C2.构建索引

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

例如,将mongo shell 连接到实例,然后使用db.collection.createIndex()方法在records集合的username字段上创建升序索引:

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

C3.重新启动程序 mongod 作为副本集成员

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

Important

确保删除skipShardingConfigurationChecks参数和disableLogicalSessionCacheRefresh参数。

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

Configuration File
Command-line Options

If you are using a configuration file:

net:
   bindIp: localhost,<hostname(s)|ip address(es)>
   port: 27018
replication:
   replSetName: shardA
sharding:
   clusterRole: shardsvr

Other settings (e.g. storage.dbPath, etc.) remain the same.

And restart:

mongod --config <path/To/ConfigFile>

If you are using command-line options:

For example:

mongod --port 27018 --replSet shardA --shardsvr

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

允许复制赶上该成员。

C4.对分片剩余的次要对象重复上述步骤

一旦该成员赶上该集合的其他成员,就一次对一个碎片的其余第二个成员重复此过程:

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

当分片的所有辅助数据库都具有新索引时,请降低分片的主数据库,使用上述过程以独立方式重新启动它,然后在前一个主数据库上构建索引:

D.对其他受影响的碎片重复

完成为分片构建索引后,对其他受影响的分片重复C.在包含集合块的碎片上构建索引

E.重新启动平衡器

一旦完成了受影响分片的滚动索引构建,请重新启动平衡器。

mongo shell 连接到分片群集中的mongos实例,然后运行sh.startBalancer()

sh.startBalancer()
首页