IllegalOperation: Attempted to create a lock file on a read-only directory MongoDB in ubuntu 20.04 - mongodb

I have just installed mongodb on my remote ubuntu server for using it with an angular and nodejs project. I created a user using the db.createUser command along with password and roles in the mongo shell.
Then when i try to start the mongodb instance with access control using the command:
mongod --auth --port 27017 --dbpath /var/lib/mongodb
its displaying the error:
IllegalOperation: Attempted to create a lock file on a read-only directory MongoDB

First of all, have you checked the permissions of the folder /var/lib/mongodb (ls -l /var/lib)?
The folder and its contents should be assigned to the mongod user. You could also check the ACL permissions (getfacl), if that's installed in your system.
If permissions seem correct, please continue reading to know how I solved the same error in a different environment.
This happened to me in CentOS 8 after a wrong reinstallation of MongoDB and the data folder /var/lib/mongo had to be created manually. In my case, the problem was that the security context of SELinux (https://www.redhat.com/en/topics/linux/what-is-selinux) had to be updated to give mongod access the folder.
The command below solved my problem (note the path to the data folder in my configuration is slightly different, just update it for your case):
chcon -Rv --type=mongod_var_lib_t /var/lib/mongo
It basically tells SELinux that the context used by MongoDB to access the files should be associated with the target folder.
You can have more information about chcon here: https://www.man7.org/linux/man-pages/man1/chcon.1.html

Related

Running an instance of mongod from terminal ends with "[initandlisten] shutting down with code:100"

I've been searching for an answer for a week now and haven't found out how to keep this from happening. I currently have mongo stored from my root directory into a file with path /data/db. The contents of this directory after typing ls...
Contents of /data/db
After I run the command mongod I see this...
After running mongod
I've installed mongodb with homebrew and have followed the installation rules over and over after installing and uninstalling thinking I made a mistake. I'm stuck and am new to mongodb altogether. I've noticed that Homebrew is installed in /usr/local/ and I believe mongo is stored in /data/db/.
I think there is some permission issue. Try changing the group and owner of the directory and the directory where socket file is being created through the command
sudo chown mongo User:mongo User data/socket directory

How to resolve MongoDB failing to instantiate [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I followed the MongoDb Docs to setup my first MongoDb,
When I start MongoDB using the command
C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe
I get the following error
exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating
shutdown: going to close listening sockets...
shutdown: going to flush diaglog...
now exiting
shutting down with code:100
MongoDB needs a folder to store the database. Create a C:\data\db\ directory:
mkdir C:\data\db
and then start MongoDB:
C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe
Sometimes C:\data\db folder already exists due to previous installation. So if for this reason mongod.exe does not work, you may delete all the contents from C:\data\db folder and execute mongod.exeagain.
For macOS users to fix this issue:
You need to go through the following steps:
Create the “db” directory. This is where the Mongo data files will live. You can create the directory in the default location by running:
sudo mkdir -p /data/db
Make sure that the /data/db directory has the right permissions by running:
sudo chown -R `id -un` /data/db
You're all set now and you can run sudo mongod to start the Mongo server.
It's not working if you run only mongod
Source.
Same issue on my Mac (using Brew) solved using:
sudo mongod
For macOS users take care of below issue:
if you installing MongoDB Community on macOS using .tgz Tarball
((Starting with macOS 10.15 Catalina, Apple restricts access to the MongoDB default data directory of /data/db. On macOS 10.15 Catalina, you must use a different data directory, such as /usr/local/var/mongodb.))
you can solve it as the following:
(MacOS Catalina onwards)
Apple created a new Volume in Catalina for security purposes. If you’re on Catalina, you need to create the /data/db folder in System/Volumes/Data.
Use this command:
sudo mkdir -p /System/Volumes/Data/data/db
Then, use this command to give permissions:
sudo chown -R `id -un` /System/Volumes/Data/data/db
this will replace normal
sudo mkdir -p /data/db
Make sure that the /data/db directory has the right permissions by running:
sudo chown -R `id -un` /data/db
once you finish and start mongoDB you can use the following in terminal:
sudo mongod --dbpath /System/Volumes/Data/data/db
To change default db folder C:\data\db in windows, the command is:
--dbpath
For example:
\mongod --dbpath C:\myfolder
Mac Users
Instead of running MongoDB with:
sudo mongod
You can use mongod instead if you:
Locate the data folder of mongodb (usually ~/data)
Add permission to read + write with sudo chmod -R ugo+rw data
If you need to use sudo when running mongodb (sudo mongod), that means you don't have read and write permission on the mongodb data folder
Please take following steps:
As other friends mentioned, you should make a directory first for your database data to be stored. This folder could be something like:
C:\mongo-data
From command line navigate to where you have installed mongodb and where mongod.exe resides. In my case the full path is:
C:\Program Files\MongoDB\Server\3.4\bin
From here run mongod.exe and pass it the path to the folder you created in step one using the flag --dbpath as follows:
mongod.exe --dbpath "C:\mongo-data"
Please Note: If you are on windows it is necessary to use double-quotes ("") in the above to run properly.
In this way you will get something like the following:
2017-06-14T12:45:59.892+0430 I NETWORK [thread1] waiting for connections on port 27017
If you use single quotes (' ') on windows, you will get:
2017-06-14T01:13:45.965-0700 I CONTROL [initandlisten] shutting down with code:100
Hope it helps to resolve the issue.
To run Mongo DB demon with mongod command, you should have a database directory, probably you need to run:
mkdir C:\data\db
Also, MongoDB need to have a write permissions for that directory or it should be run with superuser permissions, like sudo mongod.
I kept getting the following error when I tried to start mongodb (on mac os).
"shutting down with code:100"
I was using the following command:
./mongod --dbpath=~/mongo-data
The fix for me was that I didn't need the "=" sign and this was causing the error. So I did
./mongod --dbpath ~/mongo-data
Just wanted to throw this out there because the error in no way specifies that this is the problem. I almost removed the contents of the ~/mongo-data directory to see if that helped. Glad I remembered that cli args sometimes do not use the "=" sign.
first you have to create data directory where MongoDB stores data. MongoDB’s default data directory path is the absolute path \data\db on the drive from which you start MongoDB.
if you have install in C:/ drive then you have to create data\db directory. for doing this
run command in cmd
C:\>mkdir data\db
To start MongoDB, run mongod.exe.
"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath="c:\data\db"
The --dbpath option points to your database directory.
Connect to MongoDB.
"C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe"
to check all work good :
show dbs
1.If it shows error (shutting down
with code 100) that means it is not finding the desired
location of file.
1.a If its before macOS Catalina then create directory with
sudo mkdir -p /data/db and give permissions to use it
sudo chown -R id -un /data/db.
1.b if it macOS Catalina onwards then make
sudo mkdir -p /System/Volumes/data/db and give it
permissions
sudo chown -R id -un /System/Volumes/data/db.
2.Starting mongo db brew services run mongodb-community
3.Type mongod or mongod --dbpath /System/Volumes/Data/data/db
4.And if the mongod show error (shutting down with code 48) that
means the port is being already use so you can do two things
4.a Either you change the port of mongod by specifying port
number
mongod --dbpath /System/Volumes/Data/data/db —port 27018.
4.b Or You can kill the process at that port by finding
the process by
sudo lsof -i :27017
and then kill by command
kill -9
5.Repeat the step 2 and 3.
In MacOS:-
If you forgot to give the path of the previously created database while running the mongo server, the above error will appear.
sudo ./mongod --dbpath ../../mongo-data/
Reference
Note :- ./mongod && ../../mongo-data is relative path.
So you can avoid it by configuration in environment variable
Reference
For windows i've got same issue.
The fix was - i need to run command line as administrator.
if you already have the directory, check the dir permissions or try to restart mongo with sudo.
sudo brew services start mongodb
In my case, I got a similar error and it was happening because I had run mongod with the root user and that had created a log file only accessible by the root. I could fix this by changing the ownership from root to the user you normally run mongod from. The log file was in /var/lib/mongodb/journal/
I you are using Virtualbox check your VM.
docker-machine ssh
df -h
Look at dev/sda1 if you do not have any free space this may be due to a large number of images, or containers. you can remove them using "docker rm" and "docker rmi"
This exit code will also be given if you are changing MongoDB versions and the data directory is incompatible, such as with a downgrade. Move the old directory elsewhere, and create a new directory (as per the instructions given in other answers).
Aravind.
It happened with me too because I stopped the MongoDB by the Task Manager.
Creating the C:\data\db folder and starting the MongoDB at C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe worked for me, as cespon suggested, but the MongoDB didn't show any of my Databases previously created.
Then, removing the C:\data\db folder and repairing the installation with the MongoDB installer, I recovered my data and started normally the MongoDB.
(I'm very new with MongoDB, but it helped me solve this problem and recover may previews data).
typed mongod and getting error
Errors:
exception in initAndListen: NonExistentPath: Data directory /data/db not found.,
terminating
shuts down with Code 100
Then try with (create data and db folder with all permission)
mongod --dbpath=/data
use new tab and type mongo.
>use dbs
If still you are facing prob
then you can check for mac catalina: (https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x-tarball/)
for windows: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows-unattended/

Installing Mongo and chown /data/db yields "illegal user name" error

I'm installing MongoDB for a Node / Express tutorial and upon creation of the /data/db folder and the following command:
sudo chown 'id -u' /data/db
I received the following error:
chown: id -u: illegal user name
You can look for the current username and then try setting the permissions.
$ whoami
username
$ sudo chown username /data/db
Hope that helps.
The reason this failed is because your syntax was wrong and you misinterpreted the quoting from the source where you found it. You need to use a quote like this to execute the id command:
chown `id -u` /data/db
See the difference?
Also you seem to have arrived here at a misconception of how to install mongodb, and possibly have gone for a build from a tarball. The /data/db path is not required, it's just the default location.
But you generally can get a package install for your system:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
These will typically lay out an installation with a directory structure appropriate to your system conventions, and create a mongodb user under which the mongod process runs, with all appropriate permissions. They typically have a config file that you can set options in as well. The process is also typically set-up as a "service", so you don't need to manually invoke it each time you want to use.
You can specify the --dbpath option on startup or within a config file section, which of course is loaded by the --config option.
At any rate you should generally run mongod processes as their own user (not root) using the options and permissions on data and logging directories set for that user.

MongoDB not using /etc/mongodb.conf after I changed dbpath

Ever since I changed the dbpath in /etc/mongodb.conf, MongoDB has not been starting automatically, nor using the new dbpath. Prior to the change, MongoDB would be running when the computer started and I was able to simply run the command mongo to get into the console or start my Ruby on Rails server with no issues.
After I made the modification (in order to switch to a new drive with more space), the only way I can get everything to work is by manually running the command mongod --config /etc/mongodb.conf. If I don't run that, it doesn't seem like the service is running and running without the --config option give me the following error: ERROR: dbpath (/data/db/) does not exist. even though the config file says nothing about data/db.
Some other notes:
In addition to changing /etc/mongodb.conf, I moved all files out of /var/lib/mongodb and into /home/nick/appdev/mongodb.
I changed the owner and group from root to nick. Tried changing it back, but it didn't seem to fix anything.
I'm running Ubuntu 12.10 Beta 1 and Mongo 2.2.0 with Ruby on Rails 3.2.8
A late follow up on the above question...
I had a similar issue after moving the db to an ebs on ec2.
It turns out that just running mongod still directs the dbpath to /data/db/ (which exists).
The /etc/mongodb.conf is completely ignored unless specifically directed to.
I manage to work around this by using the directive --config or just the --dbpath(both work)
But was left wondering where does mongod takes it defaults from...?!
I was unable to locate and override these defaults anywhere.
Anyone ?
Note:
I am really annoyed by this behaviour of mongod...This is just bad design,and bad documentation.
It turns out that I needed to set the owner and group to mongodb. When I transferred the files to the new directory, I had set the owner and group to my user account nick and also tried root, neither of which worked.
To do so, here are the following commands:
sudo chown mongodb /home/nick/appdev/mongodb -R
sudo chgrp mongodb /home/nick/appdev/mongodb -R
To confirm that it worked, you can check the file permissions with:
ls -l /home/nick/appdev/mongodb
After checking all permission in the data, journal and log folders as suggested, my problem was solved by giving permission to a lock file in the /tmp folder
sudo chown mongod:mongod mongodb-27017.sock
I was running it as a AWS Amazon Linux instance. I figured that out by executing as the mongod user as below, and then, researching the error code. It might be useful for other troubleshooting.
sudo -S -u mongod mongod -f /etc/mongod.conf
MongoDB 1.6 is very old and the latest production version is 2.2, which contains a large amount of bug fixes and enhancements since 1.6.
Am I correct that you haven't installed 1.6 via a package manager such as yum or aptitude? I don't believe there are packages for 1.6 at present afaik. Therefore, mongod is behaving correctly as you have not started MongoDB with a control script.
Please see this link on configuration file options.

Moved Mongo Database to Different Drive: Unable to acquire lock for lockfilepath

I am in the process of moving my mongo data to a different drive. All of the data I want to move is stored in /data/db and I am moving it to a NAS (Network attached storage).
First step:
mongodump -d mydb -c mycollection -o nas/mongo-temp
This created a file tree in mongo-temp/ like so:
dump
`-- mydb
`-- mycollection.bson
1 directory, 1 file
I then stopped the mongod service and created a new /data/db directory:
/etc/init.d/mongod stop
mkdir mongo-temp/data/db
...and changed the dbpath line in /etc/mongodb.conf
dbpath=.../mongo-temp/data/db
I successfully restarted the mongo server using /etc/init.d/mongod start.
When I try to connect:
mongo
MongoDB shell version: 1.6.4
Thu May 3 09:53:23 *** warning: spider monkey build without utf8 support. consider rebuilding with utf8 support
connecting to: test
Thu May 3 09:53:24 Error: couldn't connect to server 127.0.0.1 (anon):1154
exception: connect failed
I've tried to start mongod with the command mongod --dbpath .../mongo-temp/data/db but I get an error that says:
Thu May 3 09:57:26 exception in initAndListen std::exception: Unable to acquire lock for lockfilepath: /home/dlpstats/nas-mnt/mongo-temp/data/db/mongod.lock
Removing the lockfile doesn't help. If I run the mongod command without --dbpath, the server starts fine and I am able to make queries on my old database.
First, you mentioned that you used mongodump to populate the new drive - was this just a method of backing things up or did you intend that to be the new database files? That is not how it works - mongodump output is not the same as a database file - it needs to be re-imported with mongoresore in fact. If you do a straight data file copy then the transfer will be seamless.
Then, as well as the permissions suggested by Wes in his answer, a few more things to check:
That you have shut down the old server successfully and completely - it's possible it's mis-reported error and you are getting it because it is trying to grab a port that is already open
You are using version 1.6.4 according to the mongo shell output, my guess is that you installed from the Ubuntu repo for 11.04 or similar, that is not a good option - 1.6 is very old at this point. Use the 10gen repos (http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages) or download the binaries and get a more recent version
Last but not least, when you start the mongod manually, make sure all the arguments are the same, like the port. When you connect via the mongo shell, specify the port you started the mongod on - don't rely on defaults when running into issues like this, be explicit.
I faced this problem and issuing following command solved my problem:
rm /var/lib/mongodb/mongod.lock
And then restart the mongod.
But I'm not sure is it a good solution or not.
Check the permissions for the directory and parent directories of mongo-temp. Presumably it's running as the mongodb user?
You need execute permissions on the directory (and parent directories) in order to create files there. Execute permissions on a directory allow you to list the files there, which is needed to be able to open the file for writing.