OPTIONS
翻译或纠错本页面

分片

分片是存储了集群一部分数据的 mongod 或者 replica set.所有分片存储了集群的全部数据.

通常来讲,每个分片都是一个复制集.复制集为每个分片的数据提供了冗余和高可靠性.

重要

MongoDB以 每个集合 为单位使用分片,你 必须 通过 mongos 访问开启了分片的集合,如果直接连接到某个分片,你只能看到集合的部分数据.每个分片上的数据并没有特定的顺序.MongoDB并不保证两个连续的数据块会分布在同一个分片上.

主分片

每个数据库都有一个”主分片” [1] 用来存储这个数据库中所有未开启分片的集合的数据.

Diagram of a primary shard. A primary shard contains non-sharded collections as well as chunks of documents from sharded collections. Shard A is the primary shard.

Diagram of a primary shard. A primary shard contains non-sharded collections as well as chunks of documents from sharded collections. Shard A is the primary shard.

使用 movePrimary 来改变数据库的主分片.

警告

The movePrimary command can be expensive because it copies all non-sharded data to the new shard. During this time, this data will be unavailable for other operations.

当你用之前是复制集的分片创建一个新的 sharded cluster 时,所有原来在复制集中的数据都会依然保存在这个复制集中,随后创建的数据库有可能存储在任意一个分片中.

[1]

“主分片”与 复制集 中的 primary 没有任何关系.

集群状态

mongo 终端中使用 sh.status() 获得集群状态的概览.这个概览包括了每个数据库的主分片以及分片之间 chunk 的分布,参见 sh.status() 以获得更多信息.