MongoDb DropDatabase无法正常工作

时间:2012-02-23 05:45:07

标签: mongodb

我在分片的四节点集群上有一个200gb的数据库,我想删除数据库并从节点中删除与之关联的所有文件。我正在连接我的mongos并在其上调用dropDatabase。系统返回ok,但如果调用show dbs,它将再次显示数据库,并显示它仍然占用200gb。我做错了什么?

2 个答案:

答案 0 :(得分:11)

我认为你遇到了这个问题:

https://jira.mongodb.org/browse/SERVER-4804

在大多数情况下,似乎数据库实际上已被移除,但是mongos仍然将其报告为存在。您可以通过尝试使用数据库并获取错误或直接登录分片并进行检查来验证它已消失。

该错误指的是在迁移发生时删除数据库的问题。您可以通过执行以下操作(在您自己的dbname中为sub)来解决问题的原因:

mongos> use config
switched to db config

// 1. stop the balancer
mongos> db.settings.update({_id: "balancer"}, {$set: {stopped: true}}, true)

// 2. wait for in-progress migrations to finish, this may take a few seconds
mongos> while (db.locks.findOne({_id: "balancer", state: {$ne: 0}}) != null) { sleep(1000); }

// 3. now you can safely drop the database
mongos> use <dbname>
switched to db <dbname>
mongos> db.dropDatabase()
{ "dropped" : "<dbname>", "ok" : 1 }

您可能希望在mongoses上运行flushRouterConfig以刷新配置信息:

mongos> use config
switched to db config
mongos> var mongoses = db.mongos.find()
mongos> while (mongoses.hasNext()) { new Mongo(mongoses.next()._id).getDB("admin").runCommand({flushRouterConfig: 1}) }
{ "flushed" : true, "ok" : 1 }

当然,真正的修复只会在提交修复时出现 - 看起来像是针对2.1

如果你处于破碎的状态,你可以试试这个,但这很棘手:

要“重置”分片元数据以从此问题中恢复,请尝试执行以下操作

首先,停止平衡器(如上所述)并等待迁移完成(也如上所述) 接下来,确保相关数据库上的应用服务器没有活动

现在,确保config.collections中没有以“TestCollection”开头的命名空间中的集合条目。如果是这样,请通过mongos删除这些条目:

mongos> use config
mongos> db.collections.find({_id: /^TestCollection\./})
// if any records found, delete them
mongos> db.collections.remove({_id: /^TestCollection\./})

接下来,确保config.databases中没有“TestCollection”的数据库条目,如果是,请通过mongos删除它:

mongos> use config
switched to db config
mongos> db.databases.find({_id: "TestCollection"})
// if any records found, delete them
mongos> db.databases.remove({_id: "TestCollection"})

现在,确保config.chunks中没有数据库中任何名称空间的条目(如默认的测试名称空间)。如果有,请通过mongos删除:

mongos> use config
switched to db config
mongos> db.chunks.find({ns: /^test\./})
// if any records found, delete them
mongos> db.chunks.remove({ns: /^test\./})

然后,所有mongoses的flushRouterConfig:

mongos> use config
switched to db config
mongos> var mongoses = db.mongos.find()
mongos> while (mongoses.hasNext()) { new Mongo(mongoses.next()._id).getDB("admin").runCommand({flushRouterConfig: 1}) }
{ "flushed" : true, "ok" : 1 }
...

最后,手动连接到每个分片主分区,并将数据库放在分片上(并非所有分片都可能有数据库,但最好是彻底并在所有分区上发出dropDatabase()调用

关于正在进行的迁移,您可以使用以下代码段:

// 2. wait for in-progress migrations to finish, this may take a few seconds
mongos> while (db.locks.findOne({_id: "balancer", state: {$ne: 0}}) != null) { sleep(1000); }

完成后,不要忘记重新启用平衡器:

mongos> use config
switched to db config

mongos> db.settings.update({_id: "balancer"}, {$set: {stopped: false}}, true)

答案 1 :(得分:3)

我遇到了同样的问题,并发现以普通用户身份发布db.dropDatabase失败了,但与sudo一样工作,以防万一。

相关问题