service mongod cant start, permission denied mongod.lock when changing dbpath 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

storage:
  dbPath: /home/user/mongodb/data/
  journal:

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.
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.