whinytween96
whinytween96

Reputation: 1965

MongoDB: exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating

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

Upvotes: 173

Views: 161188

Answers (18)

Ivan Chau
Ivan Chau

Reputation: 1435

On RHEL 8, and 9 (tested working to start mongod service)

Info:

  • SELinux

getenforce
Enforcing

  • Use custom non-default MongoDB data path
  • MongoDB Community Edition 7

For error in /var/log/mongodb/mongod.log

"ctx": "initandlisten" , "msg"; "DBException in initAndListen, terminating" , "attr": ("error": "Location28596: Unable to determine status of lock file in the data directory /CUSTOMPATH/mongo: boost:: filesystem: :status: Permission denied (system: 13]: \"/CUSTOMPATH/mongo/mongod.lock""

For error in sudo systemctl status mongod

failed
(code=exited, status=100)
mongod.service: Main process exited, code=exited, status=100/n/a
systemd[1]: mongod.service: Failed with result 'exit-code'.

To check:

sudo grep "denied.*mongod" /var/log/audit/audit.log | sudo audit2allow -w -a | less

To fix:

sudo grep "denied.*mongod" /var/log/audit/audit.log | sudo audit2allow -M mongod_custom
sudo semodule -i mongod_custom.pp

Then reapply MongoDB official suggested method

sudo semanage fcontext -a -t mongod_var_lib_t '/CUSTOMPATH/mongo.*'
sudo chcon -Rv -u system_u -t mongod_var_lib_t '/CUSTOMPATH/mongo'
sudo restorecon -R -v '/CUSTOMPATH/mongo'

And

sudo chown -R mongod:mongod /CUSTOMPATH/mongo

Reference:
Allowing Access: audit2allow | Red Hat Product Documentation
SELinux - Install MongoDB Enterprise on Red Hat or CentOS using .tgz Tarball

P.S.
Assisted with claude 3.7 sonnet

Upvotes: 0

Michael
Michael

Reputation: 3256

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.

Upvotes: 0

Kresimir
Kresimir

Reputation: 797

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.

Upvotes: 4

Hyder B.
Hyder B.

Reputation: 12296

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

Upvotes: 2

Vittore Marcas
Vittore Marcas

Reputation: 1215

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)

Upvotes: 0

Roger Heathcote
Roger Heathcote

Reputation: 3555

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

Upvotes: 0

Branko Markovic
Branko Markovic

Reputation: 11

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

Upvotes: 1

sagar kumar
sagar kumar

Reputation: 11

This worked for me in Ubuntu LTS 20.04:

$ sudo service mongod start

Upvotes: 0

yifan li
yifan li

Reputation: 135

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.

Upvotes: 4

Ericgit
Ericgit

Reputation: 7103

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!

Upvotes: 21

plcosta
plcosta

Reputation: 345

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

Upvotes: 14

David Noleto
David Noleto

Reputation: 163

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

just type from the terminal: sudo su mongod

Upvotes: 1

Jitendra
Jitendra

Reputation: 3185

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

Upvotes: 4

Enrique Benito Casado
Enrique Benito Casado

Reputation: 2080

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.

Upvotes: 5

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

Upvotes: 64

Bjorn Roche
Bjorn Roche

Reputation: 11469

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

Upvotes: 311

Hamdi Bayhan
Hamdi Bayhan

Reputation: 1843

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

Upvotes: 17

drrossum
drrossum

Reputation: 614

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

Upvotes: 31

Related Questions