MongoDB not working. "ERROR: dbpath (/data/db) does not exist." - node.js

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

Related

Running mongod in shell works but it does not work as a service

I am running mongod under Linux OS. I wanted to change my data directory from the default /var/lib/mongodb to another location say /nfs/mongodb.
When I run mongodb from shell(i.e. sudo /usr/bin/mongod --dbpath /nfs/mongodb) It works just fine.
Next step, I tried to run mongodb as a service(sudo service mongodb start)
I modified the file /etc/mongodb.conf and changed the line dppath=xxx to point to the new directory I created. When I run mongodb as service I get this error:
couldn't open file /nfs/mongodb/journal/j._0 for writing errno:1 Operation not permitted, terminating
Why the mongodb works in the shell and not as a service?
This is very likely because of permission issue.
When you run sudo mongod, it runs as root and can write to any directory.
In contrast, when running as a service, MongoDB typically runs as a limited-privilege user to prevent any security issue to escalate to root level access.
The solution is to chmod or chown the intended dbpath directory so that it's writeable by the service's user.
Note: you may want to check out the Production Notes for tips on running MongoDB optimally.

How do I change EC2 user from ubuntu to mongodb?

Launched Ubuntu 14.04 LTS micro instance on EC2, installed latest MongoDB following their official documentation. When I run sudo mongod --fork --dbpath /somedbpath --logpath /somelogpath followed by mongo,
I get the warning that it shouldn't be run as a root user. I did notice the installation process created a mongodb user and group and I can see them in the outputs of compgen -u and compgen -g respectively. I shutdown the mongod server and even deleted all files created in the dbpath directory. Now, I'd like to run the daemon as mongodb user, as I've learned it has all the appropriate permissions and ulimits required for optimal performance.
How do I switch from ubuntu to mongodb user, though? I ran sudo passwd mongodb and updated a password. Then I tried su mongodb, entered the password when prompted, but hitting ENTER kept me logged in as ubuntu only. I tried logging out and back in. I manually created the mongodb dir under /home since it wasn't present. I even created a .ssh dir inside that and copied the authorized_keys file from /home/ubuntu/.ssh to see if I can log into the server by ssh -i pemfile mongodb#serveruri but it throws Permission denied (publickey). I'm inclined to following the best practices and would like to be able to launch the mongo daemon as mongodb.
To solve this, I had to change permissions on the data directory (and it's files) and the log path, like so -
cd /var/log
sudo chown -R mongodb:mongodb mongodb/
cd /
sudo chown -R mongodb:mongodb data/
Then I ran the command similar to 7171u's answer, i.e,
sudo -u mongodb mongod --fork --dbpath /data/ --logpath /var/log/mongodb/mongod.log
And the warning's gone!
Run commands as mangodb user do:
sudo -u mangodb mongod --fork --dbpath /somedbpath --logpath /somelogpath
Its good that you solved it on your own, but this is still not clear to you.
go through the below mentioned link, to understand the role of each user.
http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/
mongod user is the one which doesnot have a home directory, that's why not /home, and it also does not have a shell too. check your /etc/passwd file for the mongod user.
mongod:x:993:990:mongod:/var/lib/mongo:/bin/false
this is the default entry for the mongod user in the above mentioned file.
its home directory is /var/lib/mongo
and its shell is false.

Service starts but doesn't keep running

I'm running Node.js and MongoDB on an Amazon EC2 instance.
Everything works perfectly if I run sudo mongod. I want to be able to close my ssh connection and have it still run, though, so I've been trying to get it going as a service.
I've changed the config so the db path is /data/db and done sudo chown -R mongodb:mongodb /data/db. Everything looks right permissions-wise when I do la -l.
When I try sudo service mongodb start, it says mongod start/running, process 4805, but that process is nowhere to be found in top (and, obviously, my app isn't hitting the db, or there would be no problem).
Trying without sudo, just service mongodb start, yields
start: Rejected send message, 1 matched rules; type="method_call", sender=":1.35" (uid=1000 pid=4809 comm="start mongod ") interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply="0" destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init ")`
I've tried mongod --repair as suggested in another question.
Everything else I'm seeing says it's a permissions issue, but the chown seems to have worked?
No idea what the Rejected send message is about.

Can't get mongodb to run. Tried `mongod --repair` and tried deleting lock file?

I first try to connect via mongo, and I get the following:
Error: couldn't connect to server 127.0.0.1:<port> at src/mongo/shell/mongo.js:145
exception: connect failed
First, I've tried the following, as per answers here:
removed the sudo mongod.lock file with root permission
and tried sudo -u mongodb mongod -f /etc/mongodb.conf --repair
I received the error Can't specify both --journal and --repair options.
Then, I tried just using regular mongo --repair, and got the following:
file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
What is a way around these two erorrs, or what is the way to fix this? Thanks.
By restarting the server, the problem was solved:
service mongodb restart
And after this, it started as normal.

MongoDB Service Will Not Start After Initial Setup

I am running Fedora 20 and installed MongoDB per the Red Hat installation guide on the official documentation. I was able to run the mongod daemon as a service without error the very first time but when I shut down my machine and came back, the service refused to start due to some failure.
In my log, listed after the successful run, I see this:
***** SERVER RESTARTED *****
ERROR: Cannot write pid file to /var/run/mongodb/mongod.pid: No such file or directory
If I try starting mongod or running mongod --repair manually, I get this message in a start up failure:
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
This is odd considering that in my config file in /etc/mongod.conf, the settings for the database path are as follows:
dbpath=/var/lib/mongo
Finally, if I run this command:
mongod --dbpath /var/lib/mongo
The daemon starts up just fine. However, I am unable to replicate that error free behavior for starting a service.
Can anyone tell me what exactly is wrong and how I can begin running mongod as a service?
EDIT
I get this message if I run mongod --config /etc/mongod.conf:
about to fork child process, waiting until server is ready for connections. forked process: 2702 ERROR: child process failed, exited with error number 1
The /var/run/mongodb directory did not exist, so I created and assigned it to the mongod user. That did not make much of a difference, unfortunately.
My /var/log/mongodb/mongod.log shows this message:
[initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /var/lib/mongo/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
What worked for me on Fedora 20: we need to create the temp dir on every boot, and that's handled by systemd-tmpfiles. So, create a file /lib/tmpfiles.d/mongodb.conf and put one line in it:
d /var/run/mongodb 0755 mongod mongod
That seems to handle it on restarts; if you don't want to restart right away, you can execute that with:
sudo systemd-tmpfiles --create mongodb.conf
(See the man pages for systemd-tmpfiles)
I have the same problem, I solved it temporarily, disabling SELinux, rebooted the machine, eliminated mongod.lock:
#rm /var/lib/mongo/mongod.lock
By creating the file /var/run/mongodb/mongo.pid (as mentioned in the configuration file /etc/mongod.conf):
#mkdir /var/run/mongodb
#touch /var/run/mongodb/mongod.pid
and giving 777 permissions:
#chmod 777 /var/run/mongodb/mongod.pid
and starting mongo:
#service mongod start
But the problem persists after restarting the machine. The folder and file disappear.
I've spent a while looking into this, and it appears as if the pid folder and file permissions don't work with the default daemon.
The simplest solution I've come across is disable the pid file by just putting a # in front of the line in the config file.
vi /etc/mongod.conf
find the line that says pidfilepath=/var/run/mongodb/mongod.pid and change it accordingly.
# pidfilepath=/var/run/mongodb/mongod.pid
For information on what commenting it out does check here.
http://docs.mongodb.org/manual/reference/configuration-options/#processManagement.pidFilePath
If you’re starting mongod as a service using:
sudo service mongod start
Make sure the directories defined for logpath, dbpath, and pidfilepath in your mongod.conf exist and are owned by mongod:mongod.
I was having the same problem running mongodb 3.0.4 on OpenSuse 13.2, and I found that the mongod directory under /var/run was missing. If I created the directory manually it would disappear after a reboot.
I solved it by adding the following lines to my /etc/init.d/mongod startup script:
mkdir -p /var/run/mongod
chown $MONGO_USER:$MONGO_GROUP /var/run/mongod
This worked for me in Ubuntu:
sudo kill $(sudo lsof -t -i:27017)
sudo rm -rf /tmp/mongodb-27017.sock
sudo rm -f /var/lib/mongo/mongod.lock
sudo rm -f /var/run/mongodb/mongod.pid
sudo mkdir -p /var/run/mongodb/
sudo touch /var/run/mongodb/mongod.pid
sudo chown -R mongodb:mongodb /var/run/mongodb/
sudo chown mongodb:mongodb /var/run/mongodb/mongod.pid
sudo service mongod start
i met the same issue,when i modify my mongod.conf as follow and problem resolved~
port=27017
dbpath=/usr/local/mongodb/data/db/
logpath=/usr/local/mongodb/logs
fork = true
tips: logpath is the logfile not a folder.
I just experience a similar problem on ubuntu. Encountered nearly every ERROR tips, like
child process failed, exited with error number 1
orchild process failed, exited with error number 100
or [signalProcessingThread] got signal 2 (Interrupt: 2), will terminate after current cmd ends
For many times I remove the Mongodb and try to install again, but the problem remains....
Finally I came to this way:
Backup your data first, then remove Mongodb with
#sudo apk-get autoremove mongodb-org
Find out all the files related with mongodb:
/#find -name mongo*
Delete them all with "rm" or "rmdir", including the packages in the /var/cache/... and everthing.
Then repeat the installation as the first time you did :
#echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
#sudo apt-get update
#sudo apt-get install -y mongodb-org
It will run again.
Comment below the line from your "mongo.conf" file.
pidfilepath=/var/run/mongodb/mongod.pid
Following commands solved for cent os
ERROR:
service mongod status
Error starting mongod. /var/run/mongodb/mongod.pid exists
FIXED BY:
rm /var/lib/mongo/mongod.lock
chown -R mongod:mongod /var/log/mongodb/
chown -R mongod:mongod /var/run/mongodb/
chown -R mongod:mongod /var/lib/mongo/
chmod 777 /var/run/mongodb/mongod.pid
mongod --dbpath /var/lib/mongo
We need to create the temp dir location of pidfile /var/run/mongodb that's handled by systemd-tmpfiles. So, create a file /lib/tmpfiles.d/mongodb.conf as root:
lnx#> sudo su
lnx#> cd /lib/tmpfiles.d
lnx#> echo “d /var/run/mongodb 0755 mongod mongod” > mongodb.conf
Then reboot or run this command to activate that temp directory:
lnx#>sudo systemd-tmpfiles --create mongodb.conf
Start mongod service:
lnx#> sudo systemctl start mongod.service
Bibliography: Fedora And Mongodb · l33tsource

Resources