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

Solution Verified - Updated -

Environment

  • Red Hat Satellite or Capsule 6.x

Issue

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

Resolution

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

    Before Red Hat Satellite/Capsule 6.3:

    # sed -n 's/dbpath=//p' /etc/mongod.conf
    

    # sed -n 's/logpath=//p' /etc/mongod.conf

    After Red Hat Satellite/Capsule 6.3:

    # sed -n 's/storage\.dbPath: //p'  /etc/opt/rh/rh-mongodb34/mongod.conf
    # sed -n 's/storage\.journal\.enabled: //p'  /etc/opt/rh/rh-mongodb34/mongod.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 Red Hat Satellite/Capsule processes (except mongod) are stopped. It is highly recommended to take mongo dump first (to /some/path/to/mongo_dump dir, per below command), and calculate DB size:

    # katello-service stop
    # service mongod start
    # mongodump --host localhost --out /some/path/to/mongo_dump
    # mongo pulp_database --eval "db.stats()"
    # 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 (storageSize counter from above db.stats() command), increased by 2GB.

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

    # 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 Red Hat Satellite/Capsule processes at the end:

    # 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.