翻译或纠错本页面

向集群中添加分片

在创建集群之后,或者要提高集群的性能时,都需要向集群中添加分片.如果还没有搭建集群,参见 部署一个集群.

在生产环境中,所有的分片都应该是 复制集.

注意事项

均衡

向集群中添加一个分片之后,影响了集群中 数据块 在分片间的均衡.为了达到均衡,均衡过程会开始迁移数据块.参见 /core/sharding-balancing 获得更多信息.

在 2.6 版更改: Chunk migrations can have an impact on disk space. Starting in MongoDB 2.6, the source shard automatically archives the migrated documents by default. For details, see moveChunk directory.

带宽预计

向集群中添加一个分片时,要确保集群内部节点之间有足够的带宽支持数据块的迁移,而不影响正常的数据流量.

向集群中添加一个分片

连接到一个 mongos 之后对集群进行操作.

  1. 使用 mongo 终端连接到一个 mongos .比如,如果一个 mongos 运行在 mongos0.example.net``27017``端口,使用以下命令进行连接:

    mongo --host mongos0.example.net --port 27017
    
  2. 使用 sh.addShard() 方法向集群中添加分片. sh.addShard() 每次添加一个分片,如果要添加的分片是一个复制集,需要指定复制集的名字与至少一个成员.在生产环境中,所有分片都应该是复制集.

    选项

    You can instead use the addShard database command, which lets you specify a name and maximum size for the shard. If you do not specify these, MongoDB automatically assigns a name and maximum size. To use the database command, see addShard.

    The following are examples of adding a shard with sh.addShard():

    • 有一个运行在 mongodb0.example.net27017 的mongod,它是名字为 rs1 的复制集的一个成员,要添加一个这样的分片,需要:

      sh.addShard( "rs1/mongodb0.example.net:27017" )
      
    • 要添加运行在 mongodb0.example.net27017 端口的单机 mongod ,使用以下命令:

      sh.addShard( "mongodb0.example.net:27017" )
      

    注解

    It might take some time for chunks to migrate to the new shard.