What i want to do: change dbpath of mongodb from /var/lib/mongodb to /home/user/mongodb/data

im using db version v3.4.7

what i've done

mkdir -p ~/mongodb/data
sudo chown -R mongodb:mongodb  /home/user/mongodb

change /etc/mongodb.conf

  dbPath: /home/user/mongodb/data/

sudo mongod -f /etc/mongod.conf 

this works. Also tried

sudo mongod --dbpath /home/user/mongodb/data/ 

command works fine

sudo service mongod start

command fails. checking the log file.

Permission denied: "/home/user/mongodb/data/mongod.lock", terminating

what is going on? I've tried everything i know. even tried setting chmod 777 to the lock file, and still has permission denied.


4 Answers 4


If you now command:

sudo chown -R mongodb:mongodb  /home/user/mongodb
sudo service mongod restart

It will work!

Problems here was that you gave command sudo mongod --dbpath /home/user/mongodb/data/ what created ROOT ownded files to that /home/user/mongodb/data/ directory and your "normal" mongodb user don't have right to overwrite those.

So, NEVER start your mongod as root OR if you do so, remember reset ownership of those data files back to mongodb.

  • I tried that! but apparently not all of the files were not owned by mongod. eventually my fix was going into /home/user/mongodb, cd /home/user/mongodb/data and doing sudo chmod mongodb:mongodb *, also did sudo chmod -R 775 /home/user/mongodb, not sure if needed make sure /home/user has permissions chmod 755
    – tritium_3
    Aug 9, 2017 at 21:12

I execute "sudo mongod --dbpath /path/to/mongo" on Centos 7 with Mongo 4 and that's ok but to repair or work as here recommended, you must remove and install.

When you remove you make sure that the execute the correct command to start over:

  • sudo yum remove mongodb-org* (to delete all configuration)

Then reinstall and continue:

  • sudo yum install -y mongodb-org
  • sudo systemctl stop mongod
  • mv /var/lib/mongo /{path}/mongo
  • sudo vi /etc/mongod.conf (inline dbPath change the path to "/{path}/mongo")
  • chown mongod:mongod -R /{path}/mongo/
  • sudo systemctl start mongod

Change path in mongodb in redhat 8

Eg: Your new location should be inside /new_drive

create new directory inside /new_drive

cd  /new_drive 
sudo mkdir database

make user as mongod in new path

sudo chown -R mongod:mongod database

Update SELinux policy (This is important) details here

sudo semanage fcontext -a -t mongod_var_lib_t ./database.*
sudo chcon -Rv -u system_u -t mongod_var_lib_t  /database
restorecon -R -v /database

if there is any problem add these command:

/sbin/restorecon -v /database/*
sudo ausearch -c 'mongod' --raw | audit2allow -M my-mongod
sudo semodule -X 300 -i my-mongod.pp

Restart mongo

sudo systemctl restart mongod

In my case when using mongodb-community in MacOS.

$ sudo chown -R root:admin /usr/local/var/mongodb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.