How to change the location that MongoDB uses to store its data?
To change the location used by MongoDB to store its data, you need to:
- Edit
/etc/mongod.conf
and change the linedbpath=/var/lib/mongodb
to the path that you desire, e.g.dbpath=/home/user/data/mongodb
- Update the permissions of your chosen path to allow the
mongodb
user to write to it, e.g.chown $USER -R /home/user/data/mongodb
- Restart the MongoDB service by running
sudo service mongodb restart
Note that if you have any data in the old location that you want to keep, you'll need to stop the MongoDB service first, manually move the files and then start the service again.
To stop the MongoDB server use sudo service mongodb stop