34

I just finished installing MongoDB on OSX and setup a directory structure with sudo mkdir -p /data/db.

When I attempt to run mongod from my user account it returns an error stating that my user account does not have read/write permission to /data/db.

How can I set the proper read/write permissions on my account for /data/db?

davidcondrey
  • 34,416
  • 17
  • 114
  • 136
Zoltan King
  • 1,964
  • 4
  • 18
  • 38

8 Answers8

79

Ensure that user account running mongod has the proper directory permissions. You can check which permissions are set like so:

ls -ld /data/db/

If they are set properly they should look something like this..

drwxr-xr-x X user wheel XXX Date Time /data/db/

If the permissions are set incorrectly you will likely see an error similar to this when attempting to run mongod

exception in initAndListen: XXXXX Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

To get the permissions set as they should be you can run the following command

sudo chmod 0755 /data/db && sudo chown $USER /data/db
Jonas
  • 725
  • 5
  • 23
davidcondrey
  • 34,416
  • 17
  • 114
  • 136
11
sudo chmod 777 /data/db/ 

Should work on Mac. Before running this cmd, permissions look like

drwxr-xr-x  X User  wheel  Date /data/db

after running the cmd

drwxrwxrwx  X User  wheel  Date /data/db
inokey
  • 5,434
  • 4
  • 21
  • 33
getSantsoh
  • 137
  • 1
  • 2
  • 7
  • What does `wheel` mean? Where you have `User` and `Date` in blue are those supposed to be replaced with something? – Agent Zebra Nov 10 '18 at 07:16
6

This is what worked for me. I am using a Mac, although you could try if you're using windows.

sudo chmod 777 /data/db
Tim Anishere
  • 796
  • 6
  • 7
3

There are three categories of users: owners, those in the group, and everyone else; chmod 777 /path/to/file lets everyone read/write the file, but chmod 775 /path/to/file lets only the owner/group read/write the file. Definitely use chmod 775 /path/to/file if you're developing a web server.

Some useful reading, especially for reasoning behind why 777, 775, and so on are used rather than other numbers: https://www.maketecheasier.com/file-permissions-what-does-chmod-777-means/

aaron
  • 351
  • 2
  • 10
2

None of these suggestions are correct, there's something else wrong with mongodb-org-3.4.16-1.el7.x86_64. It cannot start with a custom db path.

[root@localhost vagrant]# chmod 777 /data/ /data/mongodb
[root@localhost vagrant]# chown mongod:mongod /data/ /data/mongodb
[root@localhost vagrant]# ls -lad /data/mongodb/
drwxrwxrwx. 2 mongod mongod 6 Jul 11 15:24 /data/mongodb/
[root@localhost vagrant]#  systemctl start mongod
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
[root@localhost vagrant]# stat /data/mongodb^C
[root@localhost vagrant]# !tail
tail /var/log/mongodb/mongod.log
2018-07-11T15:39:10.786+0000 I STORAGE  [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/mongodb, terminating
2018-07-11T15:39:10.786+0000 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2018-07-11T15:39:10.786+0000 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2018-07-11T15:39:10.786+0000 I CONTROL  [initandlisten] now exiting
2018-07-11T15:39:10.786+0000 I CONTROL  [initandlisten] shutting down with code:100
[root@localhost vagrant]# rpm -qa | grep mongo
mongodb-org-tools-3.4.16-1.el7.x86_64
mongodb-org-shell-3.4.16-1.el7.x86_64
mongodb-org-server-3.4.16-1.el7.x86_64
mongodb-org-mongos-3.4.16-1.el7.x86_64
mongodb-org-3.4.16-1.el7.x86_64
b__
  • 21
  • 1
  • 6
0

Just run

sudo chown group:user /data/db

where group is a group of your user

Artem
  • 400
  • 2
  • 7
0

None of the given answers were working for me. I ended up configuring my personal OS user to be the root user, then manually went to the db folder - right clicked to view info - and added myself with read and write permissions.

suzmas
  • 1
  • 1
0

According to Mongodb University course M103 the permission level will be

sudo chmod 600 /path/to/db
Sohail
  • 587
  • 7
  • 25