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

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

First 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!

Solution 5

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

Solution 6

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

Solution 7

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.

Solution 8

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

Solution 9

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.

Solution 10

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.

Solution 11

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

just type from the terminal: sudo su mongod

Solution 12

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

Solution 13

This worked for me in Ubuntu LTS 20.04:

$ sudo service mongod start

Solution 14

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

Solution 15

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

Solution 16

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 doesnt 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)

Solution 17

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