couldn't connect to server 127.0.0.1 shell/mongo.js - linux

when i setup mongodb in my ubuntu , i try : ./mongo it show this error :
couldn't connect to server 127.0.0.1 shell/mongo.js
so what can i do ,
thanks

Manually remove the lockfile: sudo rm /var/lib/mongodb/mongod.lock
Run the repair script: sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Please note the following:
You must run this command as the mongodb user. If you run it as root,
then root will own files in /var/lib/mongodb/ that are necessary to
run the mongodb daemon and therefore when the daemon trys to run
later as the mongodb user, it won't have permissions to start. In
that case you'll get this error: Unable to create / open lock file
for lockfilepath: /var/lib/mongodb/mongod.lock errno:13 Permission
denied, terminating.
On Ubuntu, you must specify the configuration file /etc/mongodb.conf
using the -f flag. Otherwise it will look for the data files in the
wrong place and you will see the following error: dbpath (/data/db/)
does not exist, terminating.

sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
Here is all, sometimes, it takes a little while to start mongo after performing these operations.

Trying running $mongod
If you get en error such as
MongoDB shell version: 2.0.5
connecting to: test
Fri Jun 1 11:20:33 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
hisham-agil:~ hisham$ mongod
mongod --help for help and startup options
Fri Jun 1 11:24:47 [initandlisten] MongoDB starting : pid=53452 port=27017 dbpath=/data/db/ 64-bit host=hisham-agil.local
Fri Jun 1 11:24:47 [initandlisten] db version v2.0.5, pdfile version 4.5
Fri Jun 1 11:24:47 [initandlisten] git version: nogitversion
Fri Jun 1 11:24:47 [initandlisten] build info: Darwin gamma.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:48:32 PST 2012; root:xnu-1699.24.23~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Fri Jun 1 11:24:47 [initandlisten] options: {}
Fri Jun 1 11:24:47 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminating
Fri Jun 1 11:24:47 dbexit:
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to close listening sockets...
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to flush diaglog...
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to close sockets...
Fri Jun 1 11:24:47 [initandlisten] shutdown: waiting for fs preallocator...
Fri Jun 1 11:24:47 [initandlisten] shutdown: lock for final commit...
Fri Jun 1 11:24:47 [initandlisten] shutdown: final commit...
Fri Jun 1 11:24:47 [initandlisten] shutdown: closing all files...
Fri Jun 1 11:24:47 [initandlisten] closeAllFiles() finished
Fri Jun 1 11:24:47 dbexit: really exiting now
Then you've run into a basic startup error that is pretty common.
By default mongod will try to use /data/db for its database files, which in this case, does not exist.
You can't start
mongo
until you handle
mongod.
Try creating those directories and make sure they are writable by the same user that is running the mongod process.
**See similar question-- Getting an error, "Error: couldn't connect to server 127.0.0.1 shell/mongo.js" & when trying to run mongodb on mac osx lion

This is actually not an error... What happens here is that Mongo relies on a daemon in order to run the local database server, so in order to "fire up" the mongo server in your shell, you have to start the mongo service first.
For Fedora Linux (wich is the Distro I use) You have to run these commands:
1 sudo service mongod start
2 mongo
And there you have it! the server is going to run. Now, If you want Mongo service
to Start when the system boots then you have to run:
sudo chkconfig --levels 235 mongod on
And that's all! If you do that, now in the shell you just have to type mongo in order
to start the server but that's pretty much it, the problem is you have to start the SERVICE first and then the SERVER :)
P.S. The commands I posted might work on other linux distros as well, not just in fedora... In case not maybe you have to tweak some words depending on the distro you're using ;)

I got the same problem when I tried to install mongo. I got Error as,
Error
"Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84"
Solution:
First install mongod by using:
sudo apt-get install mongodb-server
Then type
mongod --dbpath /mongo/db
Then
sudo rm /var/lib/mongodb/mongod.lock
Then
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Thank You

You need to delete the lockfile mongod.lock or /var/lib/mongodb/mongod.lock on ubuntu, then you need to run mongod.exe or service mongodb start on ubuntu first, then run mongo.exe or mongo on ubuntu.

Either your mongod is not running (check using "ps" command) or it is listening on some outside IP address and not on localhost. So first check the process list if 'mongod' is running. If yes, check with "netstat -nap" for the related port.
Of course you can start mongod on the console manually or even look into the mongod logfile
(if there is one configured...depending on how you installed mongod).

First you have to make sure that all the files and directories in your /var/lib/mongodb/ folder (or whichever folder dbpath points to) belong to the mongodb user and mongodb group.
cd /var/lib/mongodb/
sudo chown mongodb filename.*
sudo chgrp mongodb filename.*
sudo chown -R mongodb directory
sudo chgrp -R mongodb directory
(Replace filename and directory with their respective names)
Then you can remove the lock, repair the database and restart the daemon as other people already mentioned:
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start

First start your mongo server by
Users-MacBook-Pro:csv1 Admin$ mongod
all output going to: /usr/local/var/log/mongodb/mongo.log
Then open another terminal window and open shell
Users-MacBook-Pro:csv1 Admin$ mongo

Also check that your root partition has enough space to start mongod.
df -h /
You'll see smth like this on mongod launch:
Mon Aug 12 17:02:59.159 [initandlisten] recover : no journal files present, no recovery needed
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] ERROR: Insufficient free space for journal files
Mon Aug 12 17:02:59.159 [initandlisten] Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
Mon Aug 12 17:02:59.159 dbexit:
Mon Aug 12 17:02:59.159 [initandlisten] shutdown: going to close listening sockets...

On Ubuntu, try this:
sudo invoke-rc.d mongodb start

It could be combination of $PATH and Permission issue.
Try following steps given below:
Update your $PATH variable to point to your MongoDB bin file. In my case brew install MongoDB to this folder:
/usr/local/Cellar/mongodb/2.4.6/
In order to update your $PATH variable, do following:
$ sudo vi /etc/paths
Then, press ‘i’ to insert text in Vi and append the your MongoDB path to the end of the ‘paths’ file and restart the terminal.
/usr/local/Cellar/mongodb/2.4.6/bin
Use ‘Esc : w q’ to save and exit from Vi editor.
Use echo to display your path variable:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/Cellar/mongodb/2.4.6/bin
Now try to check the Mongo version, if you get following, then you are on the right track!
$ mongo --version
MongoDB shell version: 2.4.6
Now we need to create the database directory. I used the default ‘/data/db’ location suggested in MongoDB docs. I also created a log directory to avoid any permission issues while Mongo tries to create any logs. Change ownership and that will do the job.
$ sudo mkdir /data/db
$ sudo mkdir /data/log
$ whoami
username
$ chown -R username /data
Now, we will create a default config file for MongoDB to be provided for the first time we run ‘mongod’ command. Now, I will also like to point out that ‘mongod’ will start a service, which will listen for incoming data connections. This is similar having ‘$service mysqld start’ executed.Let’s go ahead and create the config file. Please keep in mind that I have created very basic config file. However, you can add many other variables to configure MongoDB. This is the first time I am playing with MongoDB, so I just know as much as I read on MongoDB docs!I created ‘mongodb.conf’.
$ sudo vi /etc/mongodb.conf
Add following:
fork = true
port = 27017
quiet = true
dbpath = /data/db
logpath = /data/log/mongod.log
logappend = true
journal = true
Please note that the default port for MongoDB server is 27017. Use your own path for dbpath and logpath you created in Step – 5. Don’t forget to close and save the conf file.
Now we are all set to start our MongoDB service. Open two instances of Terminal.In Terminal 1, type in:
$ sudo mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3516
all output going to: /data/log/mongod.log
child process started successfully, parent exiting
If you get above message, then know that you have successfully started your Mongod service.
Now, to connect to it, in Terminal 2 type following:
$mongo test
MongoDB shell version: 2.4.6
connecting to: test
Server has startup warnings:
Tue Sep 3 16:55:43.527 [initandlisten]
Tue Sep 3 16:55:43.527 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>
Ignore the warnings, but you are successfully connected to the ‘test’ database! Cool!
That's all. I applied this solution, when I tried to install copy of MongoDB on my Mac for the first time. See if this help you too.
For detailed post you can go here - http://arcanebytes.com/2013/09/03/mongodb-installation-on-mac-os-x/#comment-1036112094.
I hope it helps!
Cheers,
Chinmay

I solved this problem on ubuntu 12.04 by following steps:
1) sudo rm /var/log/mongodb
2) sudo rm /var/lib/mongodb
3) I removed mongo and then installed it again
4) sudo service mongodb restart
and All is Well

For Ubuntu:
Just Open the terminal and enter the below command.
You just have to restart your mongoDB.
sudo service mongodb restart

Related

When running Mongodb got NonExistentPath: Data directory /data/db not found., terminating

The error generated when executed sudo mongod executed
2018-09-13T20:56:08.259+0530 I CONTROL [initandlisten] MongoDB starting : pid=3419 port=27017 dbpath=/data/db 64-bit host=admin
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] db version v3.6.7
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] git version: 2628472127e9f1826e02c665c1d93880a204075e
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] allocator:
tcmalloc
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] modules: none
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] build environment:
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] distmod: ubuntu1404
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] distarch: x86_64
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] target_arch: x86_64
2018-09-13T20:56:08.260+0530 I CONTROL [initandlisten] options: {}
2018-09-13T20:56:08.312+0530 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2018-09-13T20:56:08.312+0530 I CONTROL [initandlisten] now exiting
2018-09-13T20:56:08.312+0530 I CONTROL [initandlisten] shutting down with code:100
I'm a beginner in MEAN stack application development and stuck with this error. Please help me. Thanks in advance
This solution solves my problem
Make a directory as
sudo mkdir -p /data/db
That will make a directory named as db and than try to start with commands
sudo mongod
If you get another error or problem with starting mongod, You may find problem as
Failed to set up listener: SocketException: Address already in use
If you find that another error than you have to kill the running process of mongod by typing to terminal as
ps ax | grep mongod
Find the mongod running port and kill the process.
sudo kill ps_number
Another way is to make a specefic port when starting mongod as
sudo mongod --port 27018
cd ~
mongod
All you need to do above step to prevent the error.
Don't run the mongod at different local disk location.
eg. My computer has two local disk:
1.) Local disk E: - drive : (When I run mongod on E: drive or on targeting any location on E local disk then I will get error message as)
Admin#DESKTOP-CJBHIMO MINGW64 /e/Full-Stack-Developer
$ mongod
...
{"t":{"$date":"2021-03-22T11:33:26.675+05:30"},"s":"E", "c":"STORAGE", "id":20557, "ctx":"initandlisten","msg":"DBException in initAndListen, terminating","attr":{"error":"NonExistentPath: Data directory E:\\data\\db\\ not found. Create the missing directory or specify another path using (1) the --dbpath command line option, or (2) by adding the 'storage.dbPath' option in the configuration file."}}
...
{"t":{"$date":"2021-03-22T11:33:26.677+05:30"},"s":"I", "c":"CONTROL", "id":23138, "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":100}}
{"error":"NonExistentPath: Data directory E:\data\db\ not found.
2.) Local disk C: - drive : (When I run mongod on C: drive, which I recommend to use, cause, we mostly do the installation of mongodb on C: drive as per the instruction. If in your case, you installed on another drive, then run mongod on that drive's folder on hyper. Eg. as I run mongod on C:
Admin#DESKTOP-CJBHIMO MINGW64 ~/Desktop
$ mongod
{"t":{"$date":"2021-03-22T00:52:23.840-07:00"},"s":"I", "c":"STORAGE", "id":22270, "ctx":"initandlisten","msg":"Storage engine to use detected by data files","attr":{"dbpath":"C:/data/db/","storageEngine":"wiredTiger"}}
...
{"t":{"$date":"2021-03-22T11:34:59.300+05:30"},"s":"I", "c":"NETWORK", "id":23016, "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
"msg":"Storage engine to use detected by data files","attr":{"dbpath":"C:/data/db/","storageEngine":"wiredTiger"}}
This whole example is based on windows 10. It runs successful on root directory cause it is inside C: disk. To get inside root directory :
Just run
sudo mkdir -p /data/db
On the machine running the mongoDB server
Before doing any of the other solutions see my answer here.
Maybe your conf file is being rejected due to outdated options or format changes.

MongoDB - shutting down with code:100

I've been trying to set up MongoDB on my Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-22-generic x86_64) VPS and I've been running into a few problems. I am running as a user on my server that has root access but not using the root account itself.
I started to notice the problem when I was executing db.createUser() on the admin database so I could add other users and I was getting this error:
not authorized to execute command
So I then proceeded to uninstall and install again using the tutorial as provided on the website: https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
Now when I'm on stage 2 Verify that MongoDB has started successfully on the Run MongoDB Community Edition section, that is I look at my output from the file /var/log/mongodb/mongod.log, I get the following:
2017-04-14T14:23:09.309+0200 I CONTROL [main] ***** SERVER RESTARTED *****
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] MongoDB starting : pid=9691 port=27017 dbpath=/var/lib/mongodb 64-bit host=vps338741
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] db version v3.4.3
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] git version: f07437fb5a6cca07c10bafa78365456eb1d6d5e1
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] allocator: tcmalloc
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] modules: none
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] build environment:
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] distmod: ubuntu1604
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] distarch: x86_64
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] target_arch: x86_64
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log", quiet: true } }
2017-04-14T14:23:09.349+0200 I STORAGE [initandlisten]
2017-04-14T14:23:09.349+0200 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-04-14T14:23:09.349+0200 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-04-14T14:23:09.349+0200 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=464M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-04-14T14:23:09.366+0200 E STORAGE [initandlisten] WiredTiger error (2) [1492172589:366629][9691:0x7f1755946cc0], file:WiredTiger.wt, connection: /var/lib/mongodb/WiredTiger.wt: handle-open: open: No such file or directory
2017-04-14T14:23:09.367+0200 I - [initandlisten] Assertion: 28595:2: No such file or directory src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 267
2017-04-14T14:23:09.367+0200 I STORAGE [initandlisten] exception in initAndListen: 28595 2: No such file or directory, terminating
2017-04-14T14:23:09.367+0200 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-04-14T14:23:09.367+0200 I NETWORK [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2017-04-14T14:23:09.367+0200 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-04-14T14:23:09.367+0200 I CONTROL [initandlisten] now exiting
2017-04-14T14:23:09.367+0200 I CONTROL [initandlisten] shutting down with code:100
I've looked around and it is not the same error as found here, because that is to do with the fact there isn't an /data/db folder which I have. From that log I can see it is something to do with this line:
2017-04-14T14:23:09.366+0200 E STORAGE [initandlisten] WiredTiger error (2) [1492172589:366629][9691:0x7f1755946cc0], file:WiredTiger.wt, connection: /var/lib/mongodb/WiredTiger.wt: handle-open: open: No such file or directory
Which is true, when I go to /var/lib/mongodb and do ls -l it results with:
drwxr-xr-x 2 mongodb mongodb 4096 Apr 14 14:10 journal
-rw-r--r-- 1 mongodb mongodb 0 Apr 14 14:02 mongod.lock
-rw-r--r-- 1 mongodb mongodb 0 Apr 14 14:10 WiredTiger
-rw-r--r-- 1 mongodb mongodb 21 Apr 14 14:10 WiredTiger.lock
-rw-r--r-- 1 mongodb mongodb 1004 Apr 14 14:10 WiredTiger.turtle
But I don't know if I should manually create that file as not seen this problem elsewhere. I made sure that mongodb user has permissions on these files too as stated in the answer to this question, but still no luck.
I'm really at a loss as to what to do, any help would be very appreciated as this is my like fifth attempt at getting it working!
Thanks!
If you created the mkdir -p /data/db maybe there is a chance that you must start your server with the following command : sudo mongod
macOS/OSX : https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
try :
sudo service mongod start ;
then run the mongodb with:
mongo
This is a known bug in MongoDB, check the report
Your problem is not that files are missing in general, it's that they go missing when you restart after a clean shutdown. Your problem is not related to the /data/db directory but more to WiredTiger.wt file that is missing after a restart.
Oct 1 20:00:44 alpha mongod.27017[1074]: [initandlisten] WiredTiger
(2) [1443729644:306783][1074:0x7fdba8fd4bc0], file:WiredTiger.wt,
connection: /data/db/WiredTiger.wt: No such file or directory
The bug report talks about the same thing.
Try:
I would suggest try and disable WiredTiger in the config file.
MongoDB Config Options
WiredTiger
UPDATE: To disable WiredTiger you have to change the storage.engine option in your mongo config file, by default mongod uses WiredTiger but you can also use InMemory LINK
Make a directory
sudo mkdir /data/db
Provide access to the directory
sudo chown -R $USER /data/db
Then run
mongod
If you get port already in use the stop mongod, it might be running in the background
sudo service mongod stop
If the last line is
waiting for connections on port 27017
then the mongod is working
This command helped me:
mongod --dbpath /data/db --repair
for me the 'answer' was to execute the following command (since service mongod start and just mongod failed)
sudo mongod --fork --port 27017 --replSet admin --logpath /data/log/mongo.log
I did not need to turn off wired tiger
Docker container:
In my case, I was using a docker container running
mongodb:
image: 'bitnami/mongodb:latest'
user: root
command: "chown -R $USER /bitnami/mongodb"
volumes:
- '/var/www/mongodb_data:/bitnami'
environment:
- MONGODB_USERNAME=bn_parse
- MONGODB_PASSWORD=3yfQexfqcc
- MONGODB_DATABASE=bitnami_parse
I migrated the docker host to other VPS/VM (aws is too expensive).
I added temporarily the command "chown -R $USER /bitnami/mongodb" to change the owner of that folder /bitnami/mongodb. It worked.
sudo mdkir data/db
sudo service mongod stop
sudo mongod
mongosh
this fixed it for me on ubuntu 22.04 after hours and hours of hunting down solutions

MongoDB data/db not found

I do gave this path for data/db
/usr/local/Cellar/mongodb/3.2.6/data/db
The following step was made in order to create a bound to mongodb folder
sudo mongod --directoryperdb --dbpath /usr/local/Cellar/mongodb/3.2.6/data/db --logpath /usr/local/Cellar/mongodb/3.2.6/log/mongodb.log --logappend -rest
When initialize sudo mongod in terminal the following error appears:
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] MongoDB starting : pid=8107 port=27017 dbpath=/data/db 64-bit host=iMac-Krystyna-2.local
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] db version v3.2.6
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] git version: 05552b562c7a0b3143a729aaa0838e558dc49b25
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2h 3 May 2016
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] allocator: system
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] modules: none
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] build environment:
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] distarch: x86_64
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] target_arch: x86_64
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] options: {}
2016-06-08T14:45:06.970+0200 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2016-06-08T14:45:06.970+0200 I CONTROL [initandlisten] dbexit: rc: 100
Can you please explain what is wrong and maybe show some common practice in order to work correctly with MongoDB?
You need to create this directory as root
Either you need to use sudo , e.g. sudo mkdir -p /data/db
Or you need to do su - to become superuser, and then create the directory with mkdir -p /data/db
You can also give this command sudo mongod --dbpath=/var/lib/mongodb.
Open another terminal and run your mongod.
It worked for me.
try: sudo service mongod start
I had the same issue. This worked for me:
sudo mongod --dbpath ~/data/db
In my case, an incorrect conf file caused Mongo to use default settings (i.e. using /data/db as the storage path. Because /data/db doesn't exist, it complains.
Possible sources of incorrect config include:
(1) Unquoted strings. E.g. storage.dbPath should be "some/path"
(2) Incorrect bindIp. It seems an array of IPs is not supported.
So my fix was simply to use quoted strings in those places:
storage:
dbPath: "/some/path"
net:
bindIp: "127.0.0.1"
Note: My "/some/path" has permissions drwxr-xr-x (755) and is owned by mongodb/mongodb. And it works.
MongoDB try to find data/db folder under root directory of your system. try to create folder under /data/db.

MEAN.js Could not connect to MongoDB

in creating my app with MEAN.js can not do mongodb connect on port 27017 in any way.
I installed mongodb manually in directory
~$ ls -la /etc/init.d/mongod
output
lrwxrwxrwx 1 root root 56 Nov 4 20:45 /etc/init.d/mongod -> /home/myuser/projects/packages/linux-mongodb/bin/mongod
I try to run the file or `` server.js` grunt` the NASC repository command and got the following error:
Running node server.js i receive this output:
Application loaded using the "development" environment configuration
Failed to load c ++ bson extension, using pure JS version
Insecurely using http protocol
MEAN.JS application started on port 27017
Could not connect to MongoDB!
Error: connection closed
Repository URL:
https://github.com/meanjs/mean
Some hint of what might be?
I look forward personal contact.
Thanks in advance!
Link for reference:
https://stackoverflow.com/a/22557417/3332734
running cat /etc/mongod.conf
# mongod.conf
# Where to store the data.
# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb
###dbpath=/home/myuser/projects/packages/mongodb-linux/databases
#where to log
logpath=/var/log/mongodb/mongod.log
###logpath=/home/myuser/projects/packages/mongodb-linux/mongod.log
logappend=true
port = 27017
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1
# Disables write-ahead journaling
# nojournal = true
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
# Verbose logging output.
#verbose = true
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true
# Enable db quota management
#quota = true
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#diaglog = 0
# Ignore query hints
#nohints = true
# Enable the HTTP interface (Defaults to port 28017).
#httpinterface = true
httpinterface = true
# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true
# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true
# Disable data file preallocation.
#noprealloc = true
# Specify .ns file size for new databases.
# nssize = <size>
# Replication Options
# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile
Run mongo in console and retrieve output:
$ mongo
MongoDB shell version: 2.6.5
connecting to: test
2014-11-05T19:23:18.957-0200 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-11-05T19:23:18.958-0200 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
Run mongod in console and retrieve output:
$ mongod
mongod --help for help and startup options
2014-11-05T19:26:53.215-0200 [initandlisten] MongoDB starting : pid=3487 port=27017 dbpath=/data/db 64-bit host=francis-tosystems
2014-11-05T19:26:53.215-0200 [initandlisten] db version v2.6.5
2014-11-05T19:26:53.215-0200 [initandlisten] git version: e99d4fcb4279c0279796f237aa92fe3b64560bf6
2014-11-05T19:26:53.215-0200 [initandlisten] build info: Linux build8.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-11-05T19:26:53.215-0200 [initandlisten] allocator: tcmalloc
2014-11-05T19:26:53.215-0200 [initandlisten] options: {}
2014-11-05T19:26:53.215-0200 [initandlisten] exception in initAndListen: 10296
*********************************************************************
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
2014-11-05T19:26:53.215-0200 [initandlisten] dbexit:
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to close listening sockets...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to flush diaglog...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: going to close sockets...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: waiting for fs preallocator...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: lock for final commit...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: final commit...
2014-11-05T19:26:53.215-0200 [initandlisten] shutdown: closing all files...
2014-11-05T19:26:53.216-0200 [initandlisten] closeAllFiles() finished
2014-11-05T19:26:53.216-0200 [initandlisten] dbexit: really exiting now
Or run mongod
$ mongod --config /etc/mongod.conf &
[1] 3568 2014-11-05T19:28:16.867-0200 SEVERE: Failed global initialization: FileNotOpen Failed to open "/var/log/mongodb/mongod.log"
EDIT
I am no longer using MEAN.js, but I continue my studies with MongoDB and node.js
The problem was that I did not know how to start mongodb to connect to the database
This year I learned a lot about mongodb and node.js and I understood what I needed to do.
With your help I have three steps:
Open a console with mongod --dbpath /project/path-do-database/
I connected my API to mongodb, passing the right port
Open a second console by starting the node.js API with node ornodemon and passing the project's main javascript file.
Basically, you need to have 2 terminals running at the same folder.
One for mongoDB, one for your application.
On the first terminal,
1>> npm install -g meanjs
1>> yo meanjs
then fill up your application name, author name, etc.
1>> npm install
Open up a 2nd terminal to start the mongodb server
2>> mongod
Go back to the first terminal and run it
1>> npm start
You are starting your meanjs app on the same port as your mongodb server (27017). If you do not specify a port for meanjs, it will start on 3000 in a development environment.
I know it is probably late, but error logs says it all:
ERROR: dbpath (/data/db) does not exist.
I belive you need to create /data/db folders or it was created somewhere else.
It is already answered here:
mongodb Mongod complains that there is no /data/db folder

Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused

while trying this mongo command in ubuntu
I am getting this error.
ritzysystem#ritzysystem-Satellite-L55-A:~$ mongo
MongoDB shell version: 2.6.1
connecting to: test
2014-10-06T12:59:35.802+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-10-06T12:59:35.802+0530 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
how can I rectify this is anyone had the same problem.
Run the following command :
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongod restart
Connection refused to MongoDB errno 111
MacOS:
rm /usr/local/var/mongodb/mongod.lock
sudo service mongod restart
I had the same problem on Mac OS
Try to run sudo mongod and in a new terminal tab run mongo
thank you everyone I tried running mongo it was not running finally I found there was no space in my hard disc I emptied some of the space and finally re installed it the same installation procedure as followed to install as given in mongodb docs finally restarted the server and it is running.
https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu
In my case the error was due to missing /data/db folder which the mongodb uses to store your data. Please type this command $sudo mongod in your terminal. If the error message is something like :
missing data/db folder error
Simply create the folder and you are good to go.
First,
Start MongoDB:
sudo service mongod start
Then, Run:
mongo
Probably you don't have space in your hard drive.
Check it by typing in the promt df -h
Please note that mongo might fail even with 3gb available in the corresponding partition. For details, you might want to check the log: cat /var/log/mongodb/mongod.log
Make sure you have run the MongoDB's Service before you want to connect the console.
run and start the server:
$ sudo mongod
then:
$ mongo
...
>
I had the same problem on my Mac, and I installed mongodb via homebrew. I solve this problem by the homebrew services command.
First start mongodb service:
$ brew services start mongodb
Start mongodb terminal
$ mongo
Stop mongodb service:
$ brew services stop mongodb
Do this:
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongod restart
If you are on Ubuntu 16.04 which you can figure out by runnign this:
lsb_release -a
You need to Create a new file at /lib/systemd/system/mongod.service with the following contents:
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
In my case the problem was caused due to an apparent lost of permission over mongodb.lock file. I could solve the problem changing the permission with the following command :
sudo chown mongodb:mongodb /var/lib/mongodb/mongodb.lock
There follows my investigation:
I think except for disk space issuse, you should check the log in /var/log/mongodb to know the details for why mongodb start failed.
cat /var/log/mongodb/mongod.log
2016-06-26T15:26:26.642+0800 I CONTROL [main] ***** SERVER RESTARTED *****
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] MongoDB starting : pid=8130 port=27017 dbpath=/var/lib/mongodb 64-bit host=hadoop-master
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] db version v3.2.7
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] git version: 4249c1d2b5999ebbf1fdf3bc0e0e3b3ff5c0aaf2
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] modules: none
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] build environment:
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] distmod: ubuntu1404
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] distarch: x86_64
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-06-26T15:26:26.649+0800 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1,192.168.3.10", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-06-26T15:26:26.678+0800 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
2016-06-26T15:26:26.678+0800 I - [initandlisten] Fatal Assertion 28578
2016-06-26T15:26:26.678+0800 I - [initandlisten]
***aborting after fassert() failure
So, here I need to rm all the files in the /tmp. That works fine for me.
For mongo v3.6.3+ (or 2019 versions)
rm /var/lib/mongodb/mongod.lock
service mongodb restart
I understand the question is with regards to Ubuntu.
But if you're getting the same error on a Mac and don't want $ sudo mongod running on a separate tab, you can do the following to fix the connection error, if you have Homebrew -
brew services start mongodb
Based on the answer from this thread - Cannot connect to mongodb errno:61 Connection refused
In my case bind_ip was not 127.0.0.1 in /etc/mongodb.conf file so change bind_ip to 127.0.0.1(it may be comma separated values so make sure 127.0.0.1 is one of them) Then restart your system to take effect.
Restart only to the those who are facing
$sudo service mongod restart
Failed to restart mongod.service: Unit mongod.service not found.
Follow this simple steps; (Works on MAC OS too)
Open terminal and run sudo mongod
Open a new terminal tab(Don't close step 1 tab) and run sudo mongo
That's all
is very simple, only delete a file /var/lib/mongodb/mongodb.lock. after only execute: mongo. finished
It is possible that the data directory for mongo specified in /etc/mongod.conf is not a valid path.
Try rechecking the path if it really exists sudo vi /etc/mongod.conf and check dbPath.
Just some thoughts on my case.
If you have changed the dbPath and logPath dirs to your custom values (say /data/mongodb/data, /data/mongodb/log), you must chown them to mongodb user, otherwise, the non-existent /data/db/ dir will be used.
sudo chown -R mongodb:mongodb /data/mongodb/
sudo service mongod restart
I have the same error with you, this is my case:
~# mongod
2018-07-15T05:27:08.265+0000 I JOURNAL [initandlisten] journal dir=/data/db/journal
2018-07-15T05:27:08.265+0000 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2018-07-15T05:27:08.301+0000 I JOURNAL [durability] Durability thread started
2018-07-15T05:27:08.301+0000 I JOURNAL [journal writer] Journal writer thread started
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] MongoDB starting : pid=26796 port=27017 dbpath=/data/db 64-bit host=ubuntu-s-2vcpu-4gb-sfo2-01
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten]
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten]
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten]
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] db version v3.0.6
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] build info: Linux build6.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] allocator: tcmalloc
2018-07-15T05:27:08.302+0000 I CONTROL [initandlisten] options: {}
2018-07-15T05:27:08.308+0000 I NETWORK [initandlisten] waiting for connections on port 27017
I type mongod to start the server, and I type control + c to exit to shell
then I type mongo and I got
~# mongo
MongoDB shell version: 3.0.6
connecting to: test
2018-07-15T05:05:02.738+0000 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2018-07-15T05:05:02.739+0000 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179
As you can see, my error info is same with you.
This is case mongod doesn't start with backend process, when I type control + c I quit mongod.
We can add --fork args to make the process daemon process.
# mongod --logpath /usr/local/mongodb/log.txt --fork
you have to set --logpath if you want to use --fork
then you will success to connect to mongo
I faced the same problem, so please check is there mongodb folder, firstly try to remove that folder
rm -rf /var/lib/mongodb/*
Now try to start mongo and if same problem then its mongodb lock file which prevent to start mongo, so please delete the mongo lock file
rm /var/lib/mongodb/mongod.lock
For forcefully rm -rf /var/lib/mongodb/mongod.lock
service mongodb restart if already in sudo mode
otherwise you need to use like sudo service mongod start
or you can start the server using fork method
mongod --fork --config /etc/mongod.conf
and for watching the same is it forked please check using the below command
ps aux | grep mongo
Mongo 3.*.* - OSX - 2017
From README
RUNNING
Change directory to mongodb-osx-x86_64-3.*.*/bin
To run a single server database:
$ mkdir /data/db
$ ./mongod
Go to new Terminal
$ # The mongo javascript shell connects to localhost and test database
$ # by default -Run the following command in new terminal
$ ./mongo
> help
I solved that simply by entering sudo mongo after mongod command.
I just removed mongo following the instructions here: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ And then I installed again following the instructions in the given link. Everything worked smoothly!
There are changes in mongod.conf file in the latest MongoDB v 3.6.5 +
Here is how I fixed this issue on mac os High Sierra v 10.12.3
Note: I assume that you have installed/upgrade MongoDB using homebrew
mongo --version
MongoDB shell version v3.6.5
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
OpenSSL version: OpenSSL 1.0.2o 27 Mar 2018
allocator: system modules: none build environment:
distarch: x86_64
target_arch: x86_64
find mongod.conf file
sudo find / -name mongod.conf`
/usr/local/etc/mongod.conf > first result .
open mongod.conf file
sudo vi /usr/local/etc/mongod.conf
edit in the file for remote access under net: section
port: 27017
bindIpAll: true
#bindIp: 127.0.0.1 // comment this out
restart mongodb
if you have installed using brew than
brew services stop mongodb
brew services start mongodb
otherwise, kill the process.
sudo kill -9 <procssID>
Issue was with the connection to the database, it could be due to several reasons.
For me it was low disk space on my server machine, and when mongo tries to connect it refuse because of low disk, check your server machine local storage with du -sh if you have low storage space, check your logs size and take step accordingly, issue may exist because of dbpath contradict in /etc/mongod.conf and server actually running dbpath with mongod
If you are facing such type of issue, please check my answer in the given link below.
https://stackoverflow.com/a/53057695/8247133
For those with Windows. I had the same problem on Windows.
Restarting Mongodb service solves this.
Following are the steps to restart on Windows:
i). Go To "Services" on Windows.
ii). Search for "MongoDB" in the list of services.
iii). Right click "start"/"restart" depending on your status.
iv). After the status turns to "running" check if issue is resolved by running "Mongo" or connecting your Mongo client.
For macOS many of the answers are already outdated according to official docs. Somehow the brew has changed and how we should install MongoDB, firstly uninstall it from your macOS (even though it might be not necessary) and then install it using these steps:
brew tap mongodb/brew
brew install mongodb-community#4.4
brew services start mongodb-community#4.4
Watch out for the 4.4 part, it'll change. If in the future it would break then refer to the official docs and install the version, which is suggested in the linked tutorial.

Resources