硬盘空间不足,如何释放已删除的mongodb集合


菜鸟摸石头过河,使用mongodb有几个月,数据一直在不断累加,现在mongodb已经200G,我试图删除一些数据,但是mongodb并没有释放那些空间。
查资料显示需要使用 db.repairDatabase(),但是出现了一个错误:

"Cannot repair database db_wallpaper having size: 212447789056 (bytes) because free disk space is: 157012410368 (bytes)"。

硬盘空间不足以空出200G... 该怎么办...

mongo: 2.6.3

python mongodb

mikicn 10 years, 11 months ago

在100G的时候,你就该作清理工作
至少,在知道数据量大的时候,你就该准备相关的知识

阿诺猪华吃雪茄 answered 10 years, 11 months ago

官方文档上有说明:

repairDatabase requires free disk space equal to the size of your current data set plus 2 gigabytes . If the volume that holds dbpath lacks sufficient space, you can mount a separate volume and use that for the repair. When mounting a separate volume for repairDatabase you must run repairDatabase from the command line and use the --repairpath switch to specify the folder in which to store temporary repair files.

如果当前磁盘分区空间不足,可以尝试用 --repairpath 参数指定一个空间足够的分区路径

KSTTT answered 10 years, 11 months ago

比较慢的方法:可以尝试给这个数据库增加一个 replica-set,等新加的结点和老数据同步了之后就可以停掉老数据库并删除老数据,以新结点对外提供服务,此时数据库空间应该已经整理好并压缩到最小了。这个过程自己并不可控,200 GB 的话要同步多久无法预估,但是不会太影响 mongodb 继续提供服务。

比较快的方法:用 mongodump/mongoexport 导出所有数据,可以远程导出,倒完之后再用 mongorestore/mongoimport 恢复数据。这个操作会导致 mongodb 锁数据库,无法对外提供服务,如果锁了也无所谓可以用这个方法,200 GB 说不定需要小半天的时间,得自己估量一下。

另外, db.repairDatabase() 效果很有限,且会导致长时间锁数据库,不用为好。mongodb 解决磁盘问题比较好的方法是用 auto-sharding 将数据分摊到多台机器上,不过这个事情得提前计划好,一旦数据库已经非常大了,能做的事情就很少了。

最接近神的银 answered 10 years, 11 months ago

Your Answer