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

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.

Related

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

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

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/

getting an error when firing up mongodb in terminal [duplicate]

I'm getting the following error when I try to run "mongod" in the terminal. I've tried uninstalling, reinstalling, and restarting the machine. Any suggestions on how to get it working would be amazing.
ERROR:
dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
Side note:
Node also stopped working on my machine around the same time that I got this error.
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
Any help would be much appreciated!
This should work to ensure that the directory is set up in the right place so that Mongo can find it:
sudo mkdir -p /data/db/
sudo chown `id -u` /data/db
You need to create the directory on root /data/db or set any other path with the following command :
mongod --dbpath /srv/mongodb/
See the example link
I solved the problem with :
sudo mongod --dbpath=/var/lib/mongodb and then mongo to access the mongodb Shell.
Change the user of the new data directory:
chown mongodb [rute_directory]
And try another time to start the mongo service
service mongod start
I solve the same problem with this.
Daemons (usually ending with d) are normally started as services. Starting the service (daemon) will allow mongodb to work as designed (without permission changes if integrates well with your distro). I start it using the service named mongodb instead of starting mongod directly--on distro with systemd enable on startup then run like:
sudo systemctl enable mongodb
sudo systemctl start mongodb
or, on distro with upstart (if you have /etc/init) or init (if you have /etc/init.d) ( https://www.tecmint.com/systemd-replaces-init-in-linux/ ) instead run:
sudo service mongodb enable
sudo service mongodb start
If you have a distro with rc ("run commands") such as Gentoo (settings in /etc/init.d) (https://forums.gentoo.org/viewtopic-t-854138-start-0.html) run:
rc-update add mongodb default
/etc/init.d/mongodb start
In a distro/version of FreeBSD which still has rc (check whether your version switched to systemd, otherwise see below):
add the following line to /etc/rc.conf:
mongod_enable="YES"
then:
sudo service mongod start
After starting the service, an unpriveleged user can use mongo, and each user will have separate data.
I also got the error that "The file /data/db doesn't exist" when I tried to save my file using the "mkdir -p /data/db" command(using both with and without sudo command). But later on one site, a person named Emil answered that the path "/data/db" no longer works on Mac, so use "~/data/db" instead
i.e., use the command
mkdir -p ~/data/db
instead of previous command.
Moreover, use
mongod --dbpath ~/data/db
to run mongod
It worked for me, hope it work for others too facing the same problem

Mongo insecure world dir

I'm extremely new to using terminal commands. I'm trying to run an app locally on my Mac and i'm having difficulty with that so I'm trying to pick through the possible issues. The first one is the when using the mongod command I get an error of the flowing
/usr/local/bin/mongod:3: warning: Insecure world writable dir /usr/local in PATH, mode 040777
all output going to: /usr/local/var/log/mongodb/mongo.log
Does this seem to be a problem or erroneous?
Having /usr/local world writable is never a good idea. Normal permissions only allow the superuser (root) to write to these system directories.
To bring you back closer to the default run:
sudo chmod 755 /usr/local
Furthermore you might want to clear the world writable bit on every subdir and file by running:
sudo chmod -R o-w /usr/local
This will mean you have to either start mongod as root or change the database path to somewhere you can write (for example in your homedir).
I run mongod on my developer machine using the following command:
mongod --bind_ip 127.0.0.1 --dbpath ~/mongo -v --profile 2
This means the following:
--bind_ip 127.0.0.1 make sure others in your network can't access your database
--dbpath ~/mongo save database files in your home dir in the directory mongo
-v show queries that are being executed
--profile 2 shows performance and profile information on queries
You should run mongod -h to see all available options.

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.