42

Earlier i was storing all the mongodb data files in /var/lib/mongodb directory..and the dbpath entry in /etc/mongodb.conf was /var/lib/mongodb..

Now i want to change the data directory to /vol/db..so I created the directory /vol/db and changed the permissions using sudo chown -R id -u /vol/db and changed the db path entry to /vol/db in /etc/mongodb.conf

now when i start the mongodb using sudo service mongodb start..i am getting this error in /var/log/mongodb/mongodb.log

http://pastebin.com/C0tv8HQN

i need help..where I am wrong?

Mark Gill
  • 3,721
  • 5
  • 22
  • 12

5 Answers5

55

I was having the same problem, but was able to solve it thanks to a similar question. You need to make sure that /vol/db/ is owned by mongodb.

sudo chown -R mongodb:mongodb /vol/db/

If you get the error chown: invalid user: 'mongodb:mongodb', check /etc/passwd to see if there is a similar user that exists (ex. mongod).

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
kaezarrex
  • 1,246
  • 11
  • 8
19

The easiest would be

sudo chmod 777 /data/db
Federico Giust
  • 1,803
  • 4
  • 20
  • 45
  • 1
    this worked for me.. when I ran sudo chown mongodb:mongoDB /vol/db i received : "illegal group name"... this solution worked – hanzolo Jan 27 '16 at 22:50
  • 13
    This command will give very open permissions to the directory in question. The answer from @Kaezarrex (`sudo chown mongodb:mongodb /data/db`) should have the desired effect without opening the permissions so widely. – Nabil Boag May 26 '16 at 08:59
  • 7
    Never give 777 to a file, specially if it's on a production server – Juan Fuentes Jan 22 '18 at 19:40
  • 2
    yeah this "works" if you don't care about security whatsoever. – Blake Sep 18 '18 at 13:23
  • Leaving world-writable directories on a server is a bad idea... Any user can delete / edit any file in that directory – Gert van den Berg Sep 29 '20 at 05:22
7

If after changing permissions and ownership of the new db path, you're still seeing this issue, run getenforce to see if you are using a system with SELinux running in enforce mode. If getenforce returns 'enforcing', it's likely selinux is the cause of the permissions error, since mongodb is now running outside it's original context scope since the db location changed out of /var/lib/...

I don't know the details, but a brute force way then to resolve the issue without writing your own selinux policy for the new context is to simply turn off selinux :-/

sudo setenforce 0

Ideally, you'd figure out how to update the selinux policy if you're planning to run in production.

calmrat
  • 7,552
  • 3
  • 17
  • 12
2

I suggest to check what is the error by reading mongo log

tail -50 /var/log/mongodb/mongodb.log

you can immediately find the problem(including permission ones)

2

Try the following:

$ sudo chmod 755 /vol/db && sudo chown $USER /vol/db
Vinicius Santana
  • 3,936
  • 3
  • 25
  • 42
  • Add -R to this and then it worked for me, to recurse all sub files and folders: ```sudo -R chmod 755 /data/db && sudo chown -R $USER /data/db``` – Captain Fantastic May 08 '18 at 21:02