mongodb 如何在没有`db.repairDatabase()`的情况下回收已删除的空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11617862/
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
How to reclaiming deleted space without `db.repairDatabase()`?
提问by user805627
I want to shrink data files size by reclaiming deleted space, but I can't run db.repairDatabase()
, because free disk space is not enough.
我想通过回收删除的空间来缩小数据文件的大小,但我无法运行db.repairDatabase()
,因为可用磁盘空间不够。
采纳答案by Gates VP
Update: With WiredTiger, compact
doesfree space.
更新:使用 WiredTiger,可以释放空间。compact
The original answer to this question is here: Reducing MongoDB database file size
这个问题的原始答案在这里: Reducing MongoDB database file size
There really is nothing outside of repair
that will reclaim space. The compact
should allow you to go much longer on the existing space. Otherwise, you will have to migrate to a bigger drive.
除此之外真的没有任何东西repair
可以回收空间。本compact
应该让你更长的时间去对现有的空间。否则,您将不得不迁移到更大的驱动器。
One way to do this is to use an off-line secondary from your Replica Set. This should give you a whole maintenance window to migrate, repair, move back and bring back up.
一种方法是使用副本集中的离线辅助节点。这应该为您提供一个完整的维护窗口来迁移、修复、移回和恢复。
If you are not running a Replica Set, it's time to look at doing just that.
如果您没有运行副本集,那么是时候考虑这样做了。
回答by EkoostikMartin
You could run the compact
command on a single collection, or one by one in all the collections you want to shrink.
您可以compact
在单个集合上运行该命令,也可以在要缩小的所有集合中一个接一个运行该命令。
http://www.mongodb.org/display/DOCS/Compact+Command
http://www.mongodb.org/display/DOCS/Compact+Command
db.runCommand( { compact : 'mycollectionname' } )
As noted in comments, I was mistaken, compact does not actually reclaim disk space, it only defragments and rebuilds collection indexes.
正如评论中所指出的,我错了,compact 实际上并没有回收磁盘空间,它只是对集合索引进行碎片整理和重建。
Instead though, you could use "--repairpath" option if you have another drive available which has available freespace.
但是,如果您有另一个具有可用空间的可用驱动器,则可以使用“--repairpath”选项。
For example:
例如:
mongod --dbpath /data/db --repair --repairpath /data/db0
Shown here: http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/
此处显示:http: //docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/
回答by udo
You can as well do a manual mongodump and mongorestore. That's basically the same what repairDatabase does. That way you can dump and restore it to/from a different machine with sufficient disk space.
您也可以手动执行 mongodump 和 mongorestore。这与 repairDatabase 所做的基本相同。这样您就可以将它转储到/从具有足够磁盘空间的另一台机器上进行转储和恢复。
回答by davissp14
If you're running a replica-set, you will want to issue a resync on each of your secondaries, one at a time. Once this has been completed, step-down your primary and resync the newly assigned secondary.
如果您正在运行副本集,您将希望在每个辅助节点上发出重新同步,一次一个。完成此操作后,请降低您的主服务器并重新同步新分配的辅助服务器。
To resync, stop your mongod instance, delete the locals and start the process back up. Watch the logs to ensure everything starts back up properly and the resync has initiated.
要重新同步,请停止您的 mongod 实例,删除本地人并开始备份过程。观察日志以确保一切正常启动并且重新同步已启动。
If you have a lot of data / indexes, ensure your oplog is large enough, otherwise it's likely to go stale.
如果您有很多数据/索引,请确保您的 oplog 足够大,否则很可能会过时。
回答by Adam Comerford
There is one other option, if you are using a replica set, but with a lot of caveats. You can fail over to another set member, then delete the files on the now former primary and do a full resync. A full resync rewrites the files from scratch in a similar way to a repair, but you will also have to rebuild indexes. This is not to be done lightly.
如果您使用的是副本集,还有另一种选择,但有很多注意事项。您可以故障转移到另一个集合成员,然后删除现在以前的主服务器上的文件并进行完全重新同步。完全重新同步以类似于修复的方式从头开始重写文件,但您还必须重建索引。这不是轻率的。
If you go down this path, my recommendation would be to have a 3 member replica set before doing this for disk space reclamation, so that at any time when a member is syncing from scratch you have 2 set members fully functional.
如果您沿着这条路走下去,我的建议是在执行此操作之前先设置 3 个成员副本集以进行磁盘空间回收,以便在成员从头开始同步时,您有 2 个成员集完全可用。
If you do not have a replica set, I recommend creating one, with two secondaries. When you sync them initially you will be creating a nice unfragmented and unpadded versions of your data. More here:
如果你没有副本集,我建议创建一个,有两个辅助。当您最初同步它们时,您将创建一个很好的未分段和未填充的数据版本。更多在这里:
http://www.mongodb.org/display/DOCS/Replica+Set+Configuration
http://www.mongodb.org/display/DOCS/Replica+Set+Configuration