减少 MongoDB 数据库文件大小 [英] Reducing MongoDB database file size

查看:33
本文介绍了减少 MongoDB 数据库文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个曾经很大 (>3GB) 的 MongoDB 数据库.从那以后,文档被删除了,我期望数据库文件的大小相应地减少.

I've got a MongoDB database that was once large (>3GB). Since then, documents have been deleted and I was expecting the size of the database files to decrease accordingly.

但由于 MongoDB 保持分配的空间,文件仍然很大.

But since MongoDB keeps allocated space, the files are still large.

我在这里和那里读到管理命令 mongod --repair 用于释放未使用的空间,但磁盘上没有足够的空间来运行此命令.

I read here and there that the admin command mongod --repair is used to free the unused space, but I don't have enough space on the disk to run this command.

你知道我可以释放未使用空间的方法吗?

Do you know a way I can freed up unused space?

推荐答案

UPDATE: 使用 compact 命令 WiredTiger 它看起来像实际会向操作系统释放额外的磁盘空间.

UPDATE: with the compact command and WiredTiger it looks like the extra disk space will actually be released to the OS.

更新: 从 v1.9+ 开始,有一个 compact 命令.

UPDATE: as of v1.9+ there is a compact command.

此命令将执行内嵌"压缩.它仍然需要一些额外的空间,但不需要那么多.

This command will perform a compaction "in-line". It will still need some extra space, but not as much.

MongoDB 通过以下方式压缩文件:

MongoDB compresses the files by:

  • 将文件复制到新位置
  • 遍历文档并重新排序/重新解析它们
  • 用新文件替换原始文件

您可以通过运行 mongod --repair 或直接连接并运行 db.repairDatabase() 来进行这种压缩".

You can do this "compression" by running mongod --repair or by connecting directly and running db.repairDatabase().

无论哪种情况,您都需要在某处留出空间来复制文件.现在我不知道为什么你没有足够的空间来执行压缩,但是,如果你有另一台有更多空间的计算机,你确实有一些选择.

In either case you need the space somewhere to copy the files. Now I don't know why you don't have enough space to perform a compress, however, you do have some options if you have another computer with more space.

  1. 将数据库导出到另一台安装了 Mongo 的计算机(使用 mongoexport),然后您可以导入相同的数据库(使用 mongoimport).这将导致新数据库更加压缩.现在您可以停止使用新的数据库文件替换原始的 mongod 并且一切顺利.
  2. 停止当前的 mongod 并将数据库文件复制到更大的计算机并在该计算机上运行修复.然后,您可以将新的数据库文件移回原始计算机.
  1. Export the database to another computer with Mongo installed (using mongoexport) and then you can Import that same database (using mongoimport). This will result in a new database that is more compressed. Now you can stop the original mongod replace with the new database files and you're good to go.
  2. Stop the current mongod and copy the database files to a bigger computer and run the repair on that computer. You can then move the new database files back to the original computer.

目前还没有使用 Mongo 进行就地压缩"的好方法.而且 Mongo 绝对可以占用大量空间.

There is not currently a good way to "compact in place" using Mongo. And Mongo can definitely suck up a lot of space.

现在压缩的最佳策略是运行主从设置.然后你可以压缩 Slave,让它赶上并切换它们.我知道还是有点毛茸茸的.也许 Mongo 团队会想出更好的就地压缩,但我认为它在他们的名单上并不高.目前认为驱动器空间很便宜(而且通常是这样).

The best strategy right now for compaction is to run a Master-Slave setup. You can then compact the Slave, let it catch up and switch them over. I know still a little hairy. Maybe the Mongo team will come up with better in place compaction, but I don't think it's high on their list. Drive space is currently assumed to be cheap (and it usually is).

这篇关于减少 MongoDB 数据库文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆