MajorYel
MajorYel

Reputation: 389

Deleting MongoDB database and collections

I am having a space problem with mongoDB and my current ubuntu 12.04 desktop computer. I always used the default setting which means that the data is physically stored under /var/lib/mongodb. By the time the database size grew vastly and now I would like to drop some collections to have some empty space in my hard drive. I have checked this and other methods But whenever I run the remove function I keep getting the following error message:

Can't take a write lock while out of disk space

The first solution came to my mind is to set the path of mongodb to an external harddisk or a usb disk and physically remove the folder using rm -rf. Would that be a safe solution? Are there any other solutions to this problem?

Upvotes: 3

Views: 7271

Answers (1)

Malcolm Murdoch
Malcolm Murdoch

Reputation: 1085

Mongodb is very good for copying databases as all you have to do is copy the full data directory to somewhere else and then you can just point a new mongod process at it and it will work.

Therefore your steps are:

  • Find the current data directory
  • Copy this to another location
  • Delete any file that has .lock in the directory (this stops Mongo from loading up again)
  • Now start up another mongo with the following command:

    mongod --dbpath <path to new directory on new disk> --port 27020
    

This will now open up a new mongo database (but on a different port to avoid any issues with a mongod that may already be running). Now you will have a new mongod running against the new disk without needing to delete collections \ dbs. Remember that Mongodb is greedy and doesn't willingly give up disk space when you delete a collection. You'll need to run

 db.repairDatabase()

Once you're happy, you can safely delete the old mongodb data directory and then copy the new one over it.

Upvotes: 3

Related Questions