16

I edited the MongoDB config file to store it's data in /home/user/data/mongod.

But data is still written to the old directory, supposedly because the permissions to the new folder are not granted - how can I give MongoDB the necessary permissions for that directory?

1
  • 1
    How did you edit your configuration? Why do you suppose it's a problem with the permissions? Why do you think that Mongo uses the default directory instead of throwing an error? (This is a way of asking: have you checked the errors log file?) Feb 18, 2013 at 12:12

5 Answers 5

35

To change the location used by MongoDB to store its data, you need to:

  1. Edit /etc/mongod.conf and change the line dbpath=/var/lib/mongodb to the path that you desire, e.g. dbpath=/home/user/data/mongodb
  2. 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
  3. Restart the MongoDB service by running sudo service mongod stop then sudo 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

NOTE 2 to run and manage your mongod process, you will be using your operating system's built-in init system. Recent versions of Linux tend to use systemd (which uses the systemctl command), while older versions of Linux tend to use System V init (which uses the service command).

If you are unsure which init system your platform uses, run the following command:

ps --no-headers -o comm 1

based on the result which will be:

  • systemd - select the systemd (systemctl) tab below. OR
  • init - select the System V Init (service) tab below.

you will execute :

sudo systemctl start mongod

in the first case and

sudo service mongod start

if you are in the second case.

5
  • which files do i need to move?
    – chovy
    Dec 17, 2016 at 9:15
  • @chovy The files in the storage directory. Dec 17, 2016 at 12:50
  • I deleted everything in /var/lib/mongodb
    – chovy
    Dec 17, 2016 at 22:35
  • @chovy Then you've probably deleted all your data, if you had any, and have no files that you can move. Dec 21, 2016 at 13:03
  • yeah, that's fine. i didn't need to save it. wasn't sure if there were non-data files i needed in there. apparently not because it seems to work still
    – chovy
    Dec 22, 2016 at 5:59
2

I ran into same issue today and I solved the issue using following steps.

  1. Edit mongod.conf file and edit dbPath variable value.

    sudo -H gedit /etc/mongod.conf
    
  2. 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 use sudo to run the command.

1

You need to restart the daemon for changes to take effect.

sudo service mongodb restart
0
1

mv the /var/lib/mongodb(the directory you saved data) to the /path and chown mondodb:mongodb /path(the directory you want to save the data)

edit the /etc/mongod.conf

service mongod restart (if cannot connect,check the /path if not contains the mongod.lock,if it exists,delete it and restart again)

1
  • 1
    its mongodb:mongodb i guess..!! Jan 13, 2019 at 22:12
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .