翻译或纠错本页面

从集群中删除分片

要删除一个 shard ,必须确定这个分片的数据已经被迁移到了集群中的其他分片中.这篇教程描述了如何安全地迁移数据和删除分片.

这篇教程描述的是如何安全地删除 一个 分片, 不要 使用这篇教程迁移一整个集群到新系统中.要想将整个集群迁移到一个新的系统,需要挨个迁移每个分片.

要想删除一个分片,需要首先使用 mongo 终端连接到 mongos ,并使用这篇文档中的一系列任务完成删除工作:

To remove a shard, first connect to one of the cluster’s mongos instances using mongo shell. Then use the sequence of tasks in this document to remove a shard from the cluster.

Ensure the Balancer Process is Enabled

为了使得数据迁移能够成功, balancer 必须 是开启的.在 mongo 终端中使用 sh.getBalancerState() 确定这一点.参见 均衡器选项 以获得更多信息.

确定要删除的分片的名字

要确定删除分片的名字,使用 mongo 终端连接到 mongos 或者:

shards._id 字段列出了每个分片的名字.

从分片中迁移数据块

admin 数据库中,运行 removeShard 命令.运行之后会开始将这个分片的数据块”转移”到其他分片的过程,比如,对一个命名为 mongodb0 的分片,运行:

use admin
db.runCommand( { removeShard: "mongodb0" } )

这个操作是立刻返回的,返回为:

{
    "msg" : "draining started successfully",
    "state" : "started",
    "shard" : "mongodb0",
    "ok" : 1
}

Depending on your network capacity and the amount of data, this operation can take from a few minutes to several days to complete.

检查迁移的状态

检查迁移的状态,再次在 admin 数据库运行 removeShard 命令,比如,对一个命名为 mongodb0 的分片,运行:

use admin
db.runCommand( { removeShard: "mongodb0" } )

这条命令返回类似如下的输出:

{
     "msg" : "draining ongoing",
    "state" : "ongoing",
    "remaining" : {
        "chunks" : 42,
        "dbs" : 1
    },
    "ok" : 1
}

在输出结果中, remaining 文档显示的是MongoDB必须迁移到其他分片的数据块中剩余的数据块数量与”primary”在这个分片的数据库数量.

remaining 字段变为0之前,持续运行 removeShard 命令检查状态.这个命令需要在 admin 数据库上运行,在其他库可以使用 sh._adminCommand 命令操作.

迁移没有分片的数据

如果这个分片是一个或多个数据库的 primary shard ,上面会存储没有分片的数据,如果不是,则跳过 完成迁移 任务.

在集群中,没有分片的数据库只会将数据存放在一个分片上,这个分片就是这个数据库的主分片.(不同的数据库可以有不同的主分片.)

警告

Do not perform this procedure until you have finished draining the shard.

  1. To determine if the shard you are removing is the primary shard for any of the cluster’s databases, issue one of the following methods:

    在返回的文档中, databases 字段列出了所有数据库和它的主分片.比如,以下的 databases 字段显示了 products 数据库使用 mongodb0 作为主分片.

    {  "_id" : "products",  "partitioned" : true,  "primary" : "mongodb0" }
    
  2. 将数据库迁移到另一个分片,需要使用 movePrimary 命令.使用如下命令将所有的剩余的未分片的数据从 mongodb0 迁移到 mongodb1 上.

    db.runCommand( { movePrimary: "products", to: "mongodb1" })
    

    This command does not return until MongoDB completes moving all data, which may take a long time. The response from this command will resemble the following:

    { "primary" : "mongodb1", "ok" : 1 }
    

警告

这个命令直到全部数据迁移完成才会返回,可能会花费较长时间.最后返回的结果类似这样:

完成迁移

为了清除所有的元信息,并结束删除分片的过程,再次执行 removeShard , 比如,对 mongodb0 这个分片,执行:

use admin
db.runCommand( { removeShard: "mongodb0" } )

在完成时会显示出成功的信息:

{
    "msg" : "removeshard completed successfully",
    "state" : "completed",
    "shard" : "mongodb0",
    "ok" : 1
}

一旦 state 的值变为 “completed”,就可以安全地停止 mongodb0` 分片上的monod进程.