MongoDB 数据删除 - 回收磁盘空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5518581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:02:21  来源:igfitidea点击:

MongoDB data remove - reclaim diskspace

mongodbrepair

提问by griffin

Possible Duplicate:
Auto compact the deleted space in mongodb?

可能的重复:
自动压缩 mongodb 中已删除的空间?

My understanding is that on delete operations MongoDB won't free up the disk space but would reuse it as needed.

我的理解是,在删除操作中 MongoDB 不会释放磁盘空间,但会根据需要重用它。

  1. Is that correct?
  2. If not, would I have run a repair command?
  3. Could the repair be run on a live mongo instance?
  1. 那是对的吗?
  2. 如果没有,我会运行修复命令吗?
  3. 可以在实时 mongo 实例上运行修复吗?

回答by Andrew Orsich

  1. Yes it is correct.
  2. No, better to give mongodb as much disk space as possible( if mongodb can allocate more space than less disk fragmentation you will have, in additional allocating space is expensive operation). But if you wish you can run db.repairDatabase()from mongodb shell to shrink database size.
  3. Yes you can run repairDatabase on live mongodb instance ( better to run it in none peak hours)
  1. 是的,这是正确的。
  2. 不,最好给 mongodb 尽可能多的磁盘空间(如果 mongodb 可以分配更多的空间而不是你将拥有的更少的磁盘碎片,额外分配空间是昂贵的操作)。但是,如果您愿意,可以db.repairDatabase()从 mongodb shell运行以缩小数据库大小。
  3. 是的,您可以在实时 mongodb 实例上运行 repairDatabase(最好在非高峰时段运行它)

回答by Justin Jenkins

This is somewhat of a duplicate of this MongoDB question ...

这有点重复这个 MongoDB 问题......

Auto compact the deleted space in mongodb?

自动压缩mongodb中已删除的空间?

See that answer for details on how to ...

有关如何...的详细信息,请参阅该答案

  • Reclame some space
  • Use serverside JS to run a recurring job to get back space (including a script you can run ...)
  • How you might want to look into Capped Collections for some use cases!
  • 回收一些空间
  • 使用服务器端 JS 运行重复性作业以取回空间(包括您可以运行的脚本...)
  • 对于某些用例,您可能希望如何查看 Capped Collections!

Also you can see this related blog posting: http://learnmongo.com/posts/compacting-mongodb-data-files/

你也可以看到这个相关的博客帖子:http: //learnmongo.com/posts/compacting-mongodb-data-files/

回答by Mojo

I have another solution that might work better than doing db.repairDatabase() if you can't afford for the system to be locked, or don't have double the storage.

如果您负担不起系统被锁定的费用,或者没有双倍的存储空间,我还有另一种可能比执行 db.repairDatabase() 更好的解决方案。

You must be using a replica set.

您必须使用副本集。

My thought is once you've removed all of the excess data that's gobbling your disk, stop a secondary replica, wipe its data directory, start it up and let it resynchronize with the master. Repeat with the other secondaries, one at a time.

我的想法是,一旦您删除了所有占用磁盘的多余数据,停止辅助副本,擦除其数据目录,启动它并让它与主副本重新同步。与其他辅助设备重复,一次一个。

On the master, do an rs.stepDown() to hand over MASTER to one of the synched secondaries, now stop this one, wipe it, and let it resync.

在 master 上,执行 rs.stepDown() 将 MASTER 移交给同步的辅助节点之一,现在停止这个,擦除它,让它重新同步。

The process is time consuming, but it should only cost a few seconds of down time, when you do the rs.stepDown().

这个过程很耗时,但是当您执行 rs.stepDown() 时,它应该只需要几秒钟的停机时间。