169

I created /data/db in root directory and ran ./mongod:

[initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating
[initandlisten] shutdown: going to close listening sockets...
[initandlisten] shutdown: going to flush diaglog...
[initandlisten] now exiting
[initandlisten] shutting down with code:100
Piper
  • 1,266
  • 3
  • 15
  • 26
whinytween96
  • 1,905
  • 3
  • 10
  • 9
  • 4
    looks like a permissions problem. Does the user running mongod have permission to write in the /data/db directory? Share the output of `ls -ld /data/db` and `id` so we can advise. – Bjorn Roche Feb 24 '17 at 19:42
  • drwxr-xr-x 2 root root 4096 – whinytween96 Feb 24 '17 at 19:44
  • uid=1000(user) gid=1000(user) groups=1000(user),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare) – whinytween96 Feb 24 '17 at 19:45
  • @BjornRoche thanks for the `-ld` part :) – Skynet Jun 21 '18 at 08:34
  • @BjornRoche my output is `drwxrwxrwx. 4 mongod mongod 4096 Sep 17 18:18 /data/db` and I am still getting the error. Any ideas? – Blake Sep 17 '18 at 18:30
  • @Blake your mongo is probably configured to use a different directory. – Bjorn Roche Sep 18 '18 at 19:24
  • @BjornRoche i configured the data directory to be `/data`. But yes my issue was unrelated - it came form having a nonstandard mongod.service file in `/etc/systemd/system/` – Blake Sep 18 '18 at 20:04
  • In my case, i was trying to connect to 'MongoDB' using 'Node.js Command Line' but without running it as an administrator. The issue resolved after i started Node.js as an admin. – TheKingPinMirza Sep 20 '18 at 17:18
  • run with `sudo` or do it with `su`. – chichi Nov 19 '21 at 17:25

18 Answers18

309

The problem is that the directory you created, /data/db is owned by and only writable by the root user, but you are running mongod as yourself. There are several ways to resolve this, but ultimately, you must give the directory in question the right permissions. If this is for production, I would advise you to check the docs and think this over carefully -- you probably want to take special care.

However, if this is just for testing and you just need this to work and get on with it, you could try this, which will make the directory writable by everyone:

> sudo chmod -R go+w /data/db

or this, which will make the directory owned by you:

> sudo chown -R $USER /data/db
Bjorn Roche
  • 11,279
  • 6
  • 36
  • 58
64

On a Mac, I had to do the following:

sudo chown -R $USER /data/db
sudo chown -R $USER /tmp/

because there was also a file inside /tmp which Mongo also needed access

31

If your system is using SELinux, make sure that you use the right context for the directory you created:

ls -dZ /data/db/
ls -dZ /var/lib/mongo/

and clone the context with:

chcon -R --reference=/var/lib/mongo /data/db
drrossum
  • 614
  • 6
  • 10
21

enter image description hereFirst of all stop all the mongoDB services, then create a directory on / , it means root, if you don't have, and remove the port file also. give all permission to that directory, become that directory owner, run below command:

sudo service mongod stop
sudo rm -rf /tmp/mongod*
sudo mkdir -p /data/db
sudo chmod -R a+wxr /data
sudo chown -R $USER:$USER /data

Now you're done, just start the MongoDB service, if didn't help, try to change the port like:

sudo service mongod restart && mongod # if didn't help run below cmd
mongod --port 27018

Note: For me all this stuff works and hoping would work for you, guy!

Ericgit
  • 6,089
  • 2
  • 42
  • 53
17

I experienced the same problem and following solution solved this problem. You should try the following solution.

sudo mkdir -p /data/db
sudo chown -R 'username' /data/db

Hamdi Bayhan
  • 1,773
  • 4
  • 17
  • 20
14

Fix the permissions of /data/db (or /var/lib/mongodb):

sudo chown -R mongodb: /data/db

then restart MongoDB e.g. using

sudo systemctl restart mongod

In case that does not help, check your error message if you are using a data directory different to /var/lib/mongodb. In that case run

sudo chown -R mongodb: <insert your data directory here>

source

plcosta
  • 345
  • 4
  • 9
5

Nice solutions, but I wonder why nobody is giving the solution for windows.

If you are using windows you just have to "Run as Administrator" the cmd.

Enrique Benito Casado
  • 1,914
  • 1
  • 20
  • 40
  • 1
    Also, it's a bad idea to run a database as administrator/root, check other comments here [like this one](https://stackoverflow.com/questions/42446931/mongodb-exception-in-initandlisten-20-attempted-to-create-a-lock-file-on-a-rea#comment88690529_42678412) for more details – Nino Filiu Sep 16 '19 at 17:34
4

I dealt with the same problem:

Change to the MongoDB directory which contains the bin directory, and run:

sudo bin/mongod 

Running MongoDB as the root user you might be asked to enter the root password. If you still can not run the MongoDB, then check the permissions for MongoDB's data directory. Enter:

ls -ld /data

or type ls -l / for the permissions for all directories and files in the directory including the /data directory.

The permission mode for the root user should be "rwx", meaning the root user is able to read, write and execute the files. To make this happen, we use:

chmod 755 /data 

755 is a octal notation to set the permissions, of the /data directory to "rwxr-xr-x", which means the root user can read, write and execute, whereas the "group" and "everyone"are able to only read and execute.

Note: When you can't do this, type instead:

sudo chmod 755 /data   

to set the permission as the root user.

Then, when done with that step, recapture the permission modes using:

ls -ld /data

which should look like:

drwxr-xr-x  3 root  wheel  102 Mar  3 17:00 /data

You don't need to worry about the "d" at the beginning, and notice that the permissions reflect "rwxr-xr-x".

You can now change back to the MongoDB directory and type:

sudo bin/mongod  

to run MongoDB.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
yifan li
  • 135
  • 3
  • 2
    Bad idea, NEVER run it as sudo. There are better solutions that manages the user/group to mongod or mongodb depending on your distro. Do not use this solution. – Andy Jun 13 '18 at 17:57
4

If you are checking your mongodb logs and you are getting such an error then just follow steps as i did. It occurs due to permission issues with your /data/db directory. I am using ubuntu16.04 and I find solution with running two simple commands as follow.

Step 1: Give permission to /data/db directory:

sudo chmod -R 0777 /data/db

Step 2: Try to start mongod service as :

sudo service mongod start

Step 3: Check status of mongod service:

sudo service mongod status
Jitendra
  • 3,135
  • 2
  • 26
  • 42
  • 2
    Step 1 will grant read / write / execute permissions to all users. This shouldn't be used. – Jonas Franz Oct 02 '19 at 10:49
  • Thanks, @JonasFranz, for your feedback, so in that case, we can use 0755 permission for the production server. Please correct me if I am wrong. – Jitendra Oct 03 '19 at 08:15
3

If you do not need to have the database directories in root, you can create data/db in your home directory (mkdir -p ~/data/db).

Then you can start the MongoDB server with the --dbpath option:

mongod --dbpath=$HOME/data/db

(This is assuming you're starting it from a terminal.)

In fact, it does not have to be ~/data/db, it can be anywhere you want and this way you can have multiple database paths and organize everything nicely. May not be the best option for production (depending on what you want to do), but for developing works fine.

Kresimir
  • 777
  • 5
  • 20
1

you just need to run the command as a super user:

just type from the terminal: sudo su mongod

David Noleto
  • 163
  • 1
  • 4
  • 1
    Its always a bad idea to run programms like this as a superuser. Its better to fix the permissions of the folders – Jason Mar 09 '19 at 16:04
1

If you are On Windows, and you are trying to setup MongoDB, run cmd as Admnistrator is the way forward as Enrique suggested above see it here

1

In my case, I was using a custom path for my data directory and I had the wrong permission set on it.

The data folder was assigned with permission ubuntu:ubuntu. You have to use mongodb as user:group.

sudo chown -R mongodb:mongodb  /path/to/mongodb
Hyder B.
  • 10,900
  • 5
  • 51
  • 60
0

This worked for me in Ubuntu LTS 20.04:

$ sudo service mongod start
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 1
    Please explain, why you think it will work for others too! – sauhardnc May 07 '20 at 18:35
  • 2
    Please describe the resolution in more detail with more insights like explaining the actual reason, add you working code snippet and steps to perform to eradicate the issues. For more details visit StackOverflow's guidelines. – Kapil Raghuwanshi May 08 '20 at 08:58
0

Check if SElinux is enabled. If it is in enforcing mode just try with permissive mode. In case that helps you should create SElinux policies for mongodb.

You can try with audit2allow - check https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security-enhanced_linux/sect-security-enhanced_linux-fixing_problems-allowing_access_audit2allow

0

Most methods of installing mongo will create a user 'mongodb'. If you have this user you should use it rather than just hacking at permissions til everything seems to work...

cat /etc/passwd | grep mongo

Trying to run mongodb as root is a bad idea and trying to run it as a normal user will give you the kind if error messages you are seeing. Use the -u option to sudo to run programs as a different user.

sudo -u mongodb mongod --dbpath /what/ever/

Also, various instructions and tutorials require you to stop mongod, make some changes, then restart it. Depending on how you stop mongod it's lock file might still be languishing in tmp, so make sure that's gone before trying to restart the daemon...

sudo rm /tmp/mongodb-27017.sock
Roger Heathcote
  • 3,091
  • 1
  • 33
  • 39
0

wrong reason:

There is no operation permission for the folder /data/db,

currently this folder only has read-only permission.

Solution:

1.Modify permissions: ensure that the current user has read and write permissions to /data/db folder

sudo chown -r your-user-name /data/db

2.Create a database path elsewhere and change the MongoDB database path for example:

I actually planned to put the database path in a place where I can see it at all times.

I used the second method directly

#First create a database storage directory,

I built it in ~/Documents/mongodb/data

#When running in the future, enter the following command to ok

mongod --dbpath ~/Documents/mongodb/data

in addition, The following problems may occur when you configure global variables in MongoDB:

for example:

1.The default terminal of the system is generally bash, but I use zsh

Then what we have to do is clear:

We need to permanently set system environment variables in zsh.

When zsh starts, it will first read the configuration file ~/.zshrc,

so we can put the configuration information of environment variables in this configuration file

vi ~/.zshrc
Add export PATH=/usr/local/mongodb/bin:$PATH

2.The default database storage address of mongodb is /data/db,

which requires us to manually create sudo mkdir -p /data/db

Then we start the service (start the server)

mongod

If you can see the prompt [Waiting for connection 27017],

your database service has actually been started

(although there are some non-critical warnings)

or

Displaying the interface waiting for the client to connect means that the startup is successful.

If it is unsuccessful, check the location of the /data/db folder.

If it doesn’t work, delete it and create a new one.

Open the browser, enter localhost:27017 or 127.0.0.1:27017,

and a line like this will appear Word.

It looks like you are trying to access MongoDB over HTTP on the native driver port.

3.Enter MongoDB command operation

Reopen the command line and enter

mongo

The database can be operated, and now we can perform database-related operations, such as executing

show dbs

When you want to stop MongoDB, you must exit correctly,

otherwise, there will be problems connecting to the database next time.

Use the following two lines of code to complete this operation.

use admin;
db.shutdownServer();

4.Finally,

the visualization tool RoboMongo

You can choose the version that suits you to download.

Others,

If you are a Mac user, you will download the MongoDB's TGZ compressed file,

decompressed, put the decompressed file in /usr/local,

by default, in the Finder, you can't see /usr this directory (terminals are used Please be skilled),

you can see this hidden directory after you can use the Shift + Command + G input /usr/local.

What's more, What to do if the connection to MongoDB fails?

Mainly due to the abnormal shutdown last time, the lock file was generated, just delete the lock file.

sudo rm /data/db/mongod.lock

What if the execution of the sudo mongod command has been stuck?

In fact, it is not stuck, but started.

This window cannot be closed.

If you open a new window, you will find that you can execute the sudo mongo command.

How to avoid this problem?

Note: When you want to stop MongoDB, you must exit correctly,

otherwise there will be problems connecting to the database next time.

Use the following two lines of code to complete this operation. Again:

use admin;
db.shutdownServer();

If you don't download MongoDB, and if use mac, just open the terminal, use the command to into dir:cd /usr/local, and use the command to download via your hands:

sudo curl -O https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.X.X.tgz

notices:4.X.X, the version of the MongoDB.

What's more, if you download stuiod3t-2019030.dmg for macOS,

Crack that:

  1. Install the dmg file;

  2. Open the file after installation;

  3. Install the directory as shown in the figure, and drag the downloaded data-man-mongodb-ent-2019.3.0.jar into and overwrite the original one;

  4. Final execution

    sudo spctl --master-disable Finished~.

(stuiod3t-2019030.dmg for macOS Cracking: https://blog.csdn.net/m0_49337600/article/details/111415870)

Vittore Marcas
  • 1,007
  • 8
  • 11
0

If you are on windows, check the permissions on db/mongodb.lock file. Just try to rename it, for example. If it does not work - then you don't have the permissions to write.

Solutions:

  1. Run the mongo as Administrator
  2. Change the permissions of whole mongo directory to add "write" permission to your user. Make sure to do it on all the subfolders.
Michael
  • 2,835
  • 2
  • 9
  • 15