Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb Excessive Disk Space

Tags:

mongodb

my mongodb take 114g which is 85%of my disk

trying to free some space using db.repairDatabase() will fail as i don't have enough free space

i know that my data shouldn't take so much space as i used to have a big collection that took 90% of the disk.

i then drop this collection and re-inserted only 20% of its data.

how can i free some space ?

like image 524
jojo Avatar asked Dec 26 '22 19:12

jojo


1 Answers

The disk space for data storage is preallocated by MongoDB and can only be reclaimed by rebuilding the database with new preallocated files. Typically this is done through a db.repairDatabase() or a backup & restore. As you've noted, a repair requires enough space to create a new copy of the database so may not be an option.

Here are a few possible solutions, but all involve having some free space available elsewhere:

  • if there is enough free space to mongodump that database, you could mongodump, drop, and mongorestore it. db.dropDatabase() will remove the data files on disk.

  • if you are using a volume manager such as LVM or ZFS, you could add extra disk space to the logical volume in order to repair or dump & restore the database.

  • if you have another server, you could set up a replica set to sync the data without taking down your current server (which would be a "primary" in the replica set). Once the data is sync'd to a secondary server, you could then stepdown the original primary and resync the database.

Note that for a replica set you need a minimum of three nodes .. so either three data nodes, or two data nodes plus an arbiter. In a production environment the arbiter would normally run on a third server so it can allow either of the data nodes to become a primary if the current primary is unavailable. In your reclaiming disk space scenario it would be OK to run the arbiter on one of the servers instead (presumably the new server).

Replica sets are generally very helpful for administrative purposes, as they allow you to step down a server for maintenance (such as running a database compact or repair) while still having a server available for your application. They have other benefits as well, such as maintaining redundant copies of your data for automatic failover/recovery.

like image 91
Stennie Avatar answered Jan 15 '23 22:01

Stennie