How to compact MongoDB files and/or reclaim disk space in "/var/lib/mongodb" in Satellite 6?

Solution Verified - Updated -

Environment

  • Red Hat Satellite 6

Issue

  • How to compact MongoDB files and/or reclaim disk space in Satelite 6?

Resolution

  • The files stored in /var/lib/mongodb, are the data and the log files as mentioned in:

    [root@satellite ~]# sed -n 's/dbpath=//p' /etc/mongodb.conf
    [root@satellite ~]# sed -n 's/logpath=//p' /etc/mongodb.conf
    
  • All these data files are required files. To compact these database files and/or reclaim disk space below command can be used from mongo shell. Ensure first, that Satellite processes (except mongod) are stopped, and it is also worth to take a backup first:

    [root@satellite ~]# katello-service stop
    [root@satellite ~]# service mongod start
    [root@satellite ~]# mongodump --host localhost --out /some/path/to/mongo_dump
    [root@satellite ~]# service mongod stop
    

    NOTE: According to the size of your mongodb instance, the repair could take a lot of time. You're suggested to execute the commands from a tmux or screen session in order to preserve the task from stopping if you'll be disconnected.

    NOTE: The repair procedure requires extra disk space to proceed. Ensure your free disk space on the relevant logical volume is of at least the size of current mongo DB size, increased by 2GB.

    [root@satellite ~]# mongo pulp_database
    > db.stats()
    > exit
    

    This will show your total consumed space in bytes.

  • Below command is the operation be used to freeup the space of mongodb (it needs mongod service being stopped):

    [root@satellite ~]# sudo -u mongodb mongod --dbpath /var/lib/mongodb --repair 
    

    Use the --repairpath switch to specify the folder in which to store temporary repair files. Be aware that above command removes and do not save any corrupt data during the repair process.

    NOTE: Check the file listings in /var/lib/mongodb before and after repairing mongodb, the output of db.stats() above, the size(of mongodb) will be freed up and the time stamp for the files will be modified.

  • start all Satellite processes at the end:

    [root@satellite ~]# katello-service start
    

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

4 Comments

Hi,

Above command: sudo -u mongodb mongod --dbpath /var/lib/mongodb --repairpath Should be: sudo -u mongodb mongod --dbpath /var/lib/mongodb --repair And it does the same as db.repairDatabase() so only one of them is needed.

thanks, corrected the command and removed the duplicate.

Hi, is necessary to have free disk space equal to the size of your current MongoDB database plus 2 GB. Is possible to add to the guide? Thanks

Good point, I added a note there.

Thanks for feedback.