减少MongoDB数据库文件大小 [英] Reducing MongoDB database file size
问题描述
我有一个曾经很大(> 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?
推荐答案
更新:使用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.
- 将数据库导出到安装了Mongo的另一台计算机上(使用
mongoexport
),然后可以导入同一数据库(使用mongoimport
).这将导致新数据库的压缩程度更高.现在,您可以停止将原来的mongod
替换为新的数据库文件,一切顺利. - 停止当前mongod,然后将数据库文件复制到更大的计算机上,然后在该计算机上运行修复程序.然后,您可以将新的数据库文件移回原始计算机.
- Export the database to another computer with Mongo installed (using
mongoexport
) and then you can Import that same database (usingmongoimport
). This will result in a new database that is more compressed. Now you can stop the originalmongod
replace with the new database files and you're good to go. - 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.
目前最好的压缩策略是运行主从设置.然后,您可以压缩从站,让它赶上并切换它们.我知道还是有点毛.也许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屋!