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 mongod stop
thensudo service mongod start
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 mongod stop
You need to restart the daemon for changes to take effect.
sudo service mongodb restart
If you start mongodb from the command line you can use the --dbpath
argument:
mkdir mydata && mongod --dbpath mydata
You may run into some issue related with "file exists" or something, it is a well known limitation of MongoDb, more on this here. Just change the disk drive and test if now you don't have that issue again.
I ran into same issue today and I solved the issue using following steps.
-
Edit mongod.conf file and edit dbPath variable value.
sudo -H gedit /etc/mongod.conf
-
Then use following command to start mongod service
sudo mongod --dbpath "your db path"
I tried to run above command without
sudo
and I got an error. So usesudo
to run the command.