mongo - couldn't connect to server 127.0.0.1:27017 - mongodb

I am coming from riak and redis where I never had an issue with this services starting, or to interact.
This is a pervasive problem with mongo and am rather clueless. Restarting does not help.I am new to mongo.
mongo
MongoDB shell version: 2.2.1
connecting to: test
Fri Nov 9 16:44:06 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed
This is what I see in the logs.
now open)
Fri Nov 9 16:44:34 [conn47] end connection 10.29.16.208:5306 (1 connection now open)
Fri Nov 9 16:45:04 [initandlisten] connection accepted from 10.29.16.208:5307 #48 (2 connections now open)
Fri Nov 9 16:45:04 [conn48] end connection 10.29.16.208:5307 (1 connection now open)
Fri Nov 9 16:45:04 [initandlisten] connection accepted from 10.29.16.208:5308 #49 (2 connections now open)
Fri Nov 9 16:45:04 [conn49] end connection 10.29.16.208:5308 (1 connection now open)
Fri Nov 9 16:45:34 [initandlisten] connection accepted from 10.29.16.208:5316 #50 (2 connections now open)
Fri Nov 9 16:45:34 [conn50] end connection 10.29.16.208:5316 (1 connection now open)
Fri Nov 9 16:45:34 [initandlisten] connection accepted from 10.29.16.208:5317 #51 (2 connections now open)
Fri Nov 9 16:45:34 [conn51] end connection 10.29.16.208:5317 (1 connection now open)
Fri Nov 9 16:46:04 [initandlisten] connection accepted from 10.29.16.208:5320 #52 (2 connections now open)
Fri Nov 9 16:46:04 [conn52] end connection 10.29.16.208:5320 (1 connection now open)
Fri Nov 9 16:46:04 [initandlisten] connection accepted from 10.29.16.208:5321 #53 (2 connections now open)
Fri Nov 9 16:46:04 [conn53] end connection 10.29.16.208:5321 (1 conn

Normally this caused because you didn't start mongod process before you try starting mongo shell.
Start mongod server
mongod
Open another terminal window
Start mongo shell
mongo

Resolved.
This problem could be solved by the below mentioned 4 steps
1) Remove .lock file
sudo rm /var/lib/mongodb/mongod.lock
2) repair the mongodb
mongod -–repair
3) start the mongod server
sudo service mongod start
4) start the mongo client
mongo
For more details take a look at http://shakthydoss.com/error-couldnt-connect-to-server-127-0-0-127017-srcmongoshellmongo-js-exception-connect-failed/
http://shakthydoss.com/technical/error-couldnt-connect-to-server-127-0-0-127017-srcmongoshellmongo-js-exception-connect-failed/

This method only works if you want to repair your data files without preserving the original files
To find where you dbpath resides- vim /etc/mongodb.conf
check for option dbpath=
(I have dbpath=/var/lib/mongodb)
Default: /data/db/
Typical locations include: /srv/mongodb, /var/lib/mongodb or /opt/mongodb .
Replace the /var/lib/mongodb with your dbpath
sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --dbpath /var/lib/mongodb/ --repair
sudo mongod --dbpath /var/lib/mongodb/ --journal
(Make sure that you leave you terminal running in which you have run above lines,
dont press 'Ctrl+c' or quit it.)
Type the command to start mongo now in another window.
Hope this works for you !
for those who want to repair your data files while preserving the original files
mongo recover

This error is what you would see if the mongo shell was not able to talk to the mongod server.
This could be because the address was wrong (host or IP) or that it was not running. One thing to note is the log trace provided does not cover the "Fri Nov 9 16:44:06" of your mongo timestamp.
Can you:
Provide the command line arguments (if any) used to start your
mongod process
Provide the log file activity from the mongod startup as well as
logs during the mongo shell startup attempt?
Confirm that your mongod process is being started on the same
machine as the mongo shell?

I am a windows user and I installed MongoDB in November 2018 and I didn't wanted to setup data/db directories. But after few days, when I open, got an error message:
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
2018-12-05T20:42:40.108+0530 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it. :
connect#src/mongo/shell/mongo.js:257:13
#(connect):1:6
exception: connect failed
Then I tried to fix using all of above answers and didn't worked. When I tried to run mongod it said
MongoDB starting : pid=12220 port=27017 dbpath=C:\data\db\ 64-bit host=bla
I tried to change the db path(to program files) through file system and also using cmd but those didnot worked.
Solution that worked for me is:
Opened the Task Manager (ctrl + shift + esc) -> Services tab and there was a MongoDB row with the status stopped. Then I right clicked and clicked start and everything working perfectly :).

MAC OS
Check status
brew services list
Something like:
Name Status User Plist
mongodb stopped
start it
brew services start mongodb
Try it again
mongo or mongo --port 27017

Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it.
This error caused because Mongo Server been closed
Simple Follow these steps
Open Task Manager
Go to >Services
Find MongoDB
Right click and select Start

In my case:
First I open config file
sudo vi /etc/mongodb.conf
Comment IP and Port like this
#bind_ip = 127.0.0.1
#port = 27017
Then restart mongodb
sudo service mongodb restart
sudo service mongod restart
Finally, it's work :D
finn#Finn ~ $ mongo
MongoDB shell version: 2.4.9
connecting to: test
> exit

A little to late with the answer but I've encountered the same issue
Following are the steps which helped me.
Go to C:\
Create "data" folder
In "data" create "db" folder
open terminal (CMD) -> go to EX:"c:\MongoDB\Server\3.4\bin"
type in mongod => this will start mongo server (leave it opened)
Run using a GUI, EX "robomongo"
Or
open new terminal (CMD) -> go to EX:"c:\MongoDB\Server\3.4\bin"
type in "mongo" command

Make sure that your mongo is running.
I fixed this problem by trying to repair the mongodb, there I found that the directory required for the db to run was not created. It shows this error
Type: mongod, it will show the error
exception in initAndListen: 29 Data directory /data/db not found., terminating
Error happen because dbpath /data/db/ (default config) does not exist. You need to create data folder and set permission for it.
Then I create a folder on my system by the command
sudo mkdir -p /data/db/
and
sudo chown id -u /data/db
then I run the mongo again and it worked.

I face same issue after searching a lot I solve this by following :
sudo service mongodb stop
sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --repair --dbpath /var/lib/mongodb
sudo mongod --fork --logpath /var/lib/mongodb/mongodb.log --dbpath /var/lib/mongodb
sudo service mongodb start

If all the above solution does not work: go to service (start>search>services) and start mongodb service. Then, in a cmd prompt, after going to bin, type :/>mongo

To connect with mongo we have to first start 'mongod' services.
following output you can see :
$ mongod
2017-03-05T00:31:39.055+0530 I CONTROL [initandlisten] MongoDB starting : pid=1481 port=27017 dbpath=/data/db 64-bit host=Prabhu-Nandans-Mac.local
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] db version v3.4.2
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2k 26 Jan 2017
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] allocator: system
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] modules: none
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] build environment:
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] distarch: x86_64
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] target_arch: x86_64
2017-03-05T00:31:39.056+0530 I CONTROL [initandlisten] options: {}
2017-03-05T00:31:39.056+0530 W - [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
2017-03-05T00:31:39.057+0530 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-03-05T00:31:39.057+0530 W STORAGE [initandlisten] Recovering data from the last clean checkpoint.
2017-03-05T00:31:39.057+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3584M,session_max=20000,eviction=(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-03-05T00:31:39.620+0530 I CONTROL [initandlisten]
2017-03-05T00:31:39.620+0530 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-03-05T00:31:39.620+0530 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-03-05T00:31:39.620+0530 I CONTROL [initandlisten]
2017-03-05T00:31:39.620+0530 I CONTROL [initandlisten]
2017-03-05T00:31:39.620+0530 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2017-03-05T00:31:39.643+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-03-05T00:31:39.643+0530 I NETWORK [thread1] waiting for connections on port 27017
2017-03-05T00:31:40.008+0530 I FTDC [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
2017-03-05T00:32:03.832+0530 I NETWORK [thread1] connection accepted from 127.0.0.1:49806 #1 (1 connection now open)
2017-03-05T00:32:03.833+0530 I NETWORK [conn1] received client metadata from 127.0.0.1:49806 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.2" }, os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "16.4.0" } }
2017-03-05T00:32:08.376+0530 I - [conn1] end connection 127.0.0.1:49806 (1 connection now open)
After that open another terminal and just type 'mongo'.
Following is output you can see :
$ mongo
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2
Your issue solved :)

I also have the same problem then I got fixed by these two lines of code. Hope, it helps
systemctl start mongod
systemctl enable mongod

Start monogdb service using
sudo service mongod start
then from the new window or terminal start mongo client using
mongo

After exhausting lots and lots of sulotions, I did-
sudo brew services start mongodb-community rather than
sudo service mongod start and it worked.
So, first try
sudo brew services start mongodb-community
and then to start the mongo shell, do-
mongo

FOR MACOS
Check the status of the services:
brew services
If you have something like:
Name Status
mongodb-community error
So you can just to stop and restart the service like this:
brew services stop mongodb-community
brew services start mongodb-community

This was happening with me today and I resolved it in following way.
Machine: I'm using Windows 10 machine and downloaded the latest MongoDB - Community edition.
So, the problem was, I did not have C:\data\db created.
Without creating C:\data\db, I opened CMD terminal and started the database using mongod command at terminal
C:\YourInstallationPath\bin>mongod
And when I fired mongo command then I was getting the problem.
Twist, I created the necessary folders but still was getting the problem. And this is because mongo server was already running. To fix it, I fired up mongod command again, and it automatically referenced to C:\data\db.
Other users have suggested to add C:\data\db but did not talk about executing mongod again, which exactly solved my problem.

I had same problem to connect mongodb after install using brew on macOS.
The problem was also mongod.lock but before i had mongodb 3.6 installed and database directory was /usr/local/var/mongodb and it have old files and mongod.lock
Simple i moved my old database files to different folder and removed everything from /usr/local/var/mongodb
Then restarted :
brew services restart mongodb
And it's working fine now.

I was getting this error on ubuntu but you can try with the following command:
sudo service mongod start

You can try with following command:
sudo service mongod start

in my case in windows and ubuntu, i needed to create /data/db folder in root. In ubuntu, i needed one additional option; to give permissions for directory.
windows
mkdir c:/data/db
ubuntu:
sudo mkdir -p /data/db
sudo chmod 700 /data/db
then run
sudo service mongod start
check
sudo service mongod status
and then run
mongo

Remove mongod.lock file. And run "mongod -repair". uninstall/reinstall mongod service fixed that problem for me.
Thanks.

Another solution that resolved this same error for me, though this one might only be if you are accessing mongo (locally) via a SSH connection via a virtual machine (at least that's my setup), and may be a linux specific environment variable issue:
export LC_ALL=C
Also, I have had more success running mongo as a daemon service then with sudo service start, my understanding is this uses command line arguments rather then the config file so it likely is an issue with my config file:
mongod --fork --logpath /var/log/mongodb.log --auth --port 27017 --dbpath /var/lib/mongodb/admin

Create the folder C:data/db if it doesn't already exist on your computer. Turns out MongoDB requires the existence of this folder structure 'data/db' to run. I hope this helps someone.

The cause by me was the space - run this in the console:
df -h
or more specifically:
du -hs /var/lib/mongodb/
to check he disk usage. If it's ~ 99% - just clean up some space and try again!

First, go to c:\mongodb\bin> to turn on mongoDB, if you see in console that mongo is listening in port 27017, it is OK
If not, close console and create folder c:\data\db and start mongod again

if all the solution above doesn't work out perhaps then this might be one to help. Somewhere down the line you may have made connection to your local mongodb database via vagrant boxes.
vagrant up and then ssh into your vagrant machine in which you've made connection with mongodb (vagrant ssh vagrant_box_name)
comment out the bind_ip line from /etc/mongod.conf using # (sudo nano /etc/mongod.conf )
restart your mongodb daemon service (sudo service mongod restart)
voila...

OK, this may be strange. My error happened because my log grew exponentially and clogged up server space hence mongo didn't get enough space to store anything.
so I cleared the log
> /var/log/mongod.log

When we take taskbar manager, go to Service tab. There MongoDB status shows the stopped. Right click for start. Its will connect MongoDB server version.
for checking type "db".
db
test
It will work fine.

Related

SocketException: Address already in use MONGODB

i found this error when trying to run mongodb. I install it via homebrew. Please assist
Agungs-MacBook-Pro:~ agungmahaputra$ mongod
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] MongoDB starting : pid=5189 port=27017 dbpath=/data/db 64-bit host=Agungs-MacBook-Pro.local
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] db version v3.6.0
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] git version: a57d8e71e6998a2d0afde7edc11bd23e5661c915
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2n 7 Dec 2017
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] allocator: system
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] modules: none
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] build environment:
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] distarch: x86_64
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] target_arch: x86_64
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] options: {}
2017-12-26T15:31:15.911+0700 E STORAGE [initandlisten] Failed to set up listener: SocketException: Address already in use
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] now exiting
2017-12-26T15:31:15.911+0700 I CONTROL [initandlisten] shutting down with code:48
Agungs-MacBook-Pro:~ agungmahaputra$
You can kill the previous mongod instance and start the new one.
To kill the previous mongod instance, first search for a list of tasks running on your machine by typing,
sudo lsof -iTCP -sTCP:LISTEN -n -P
Search for mongod COMMAND and its PID and type,
sudo kill <mongo_command_pid>
Now start your mongod instance by typing,
mongod
You can see MongoDB running successfully.
You already have a process running in the port 27017 which is used by mongodb. So either you need to stop the process in that port or try with different port number.
Try mongod --port 27018
You can change the port number of your choice.
EDIT:
You can also just stop all the running instances of mongo server using
sudo killall mongod as mentioned by #Dassi Orleando in the comments.
And run mongod
Summary other(#Tony Roczz, #Balasubramani M) answer to here:
Root Cause
error: Failed to set up listener: SocketException: Address already in use
means:
previously have run mongodb
probably use default port 27017
Solution
(most case) kill and restart mongod
kill mongod
two method:
use killall
killall mongod
kill by PID
find mongod PID
method 1: ps aux | grep mongod
output can see like this: limao 425 ... /usr/local/opt/mongodb-community/bin/mongod --config /usr/local/etc/mongod.conf
method 2: lsof -iTCP -sTCP:LISTEN -n -P
output can see like this: mongod 425 limao .. TCP 127.0.0.1:27017 (LISTEN)
kill by PID: kill -9 425
re-start mongod
most case: mongod
some special case
brew services start mongodb-community
for install community version in Mac by: brew install mongodb-community
(for some people) run with another port
run with another port: mongod --port 27018
only for those want to run multiple mongod server same time
Additional Note
which port the running mongodb is using?
 lsof -iTCP -sTCP:LISTEN -n -P
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 425 limao 10u IPv4 0xab744305e75263cf 0t0 TCP 127.0.0.1:27017 (LISTEN)
can see 27017 is current running mongodb port.
how to check / make sure mongod is running
check mongod status:
ps aux | grep mongod
output can see mongod service
brew services list
if installed by brew and start by brew
if you're using mongodb version 4.*
in default settings, there should be a "db" folder "/data/db" having the necessary permission.
to check it the MongoDB service started
brew services list
if not then run
brew services start mongodb
run mongo instead of mongod
I solved it pasting the command below into the command line in order to close mongo's server.
sudo pkill -f mongod
"sudo" (Super User Do) runs the command admin permissions, so it will ask you for your user password
First, shutdown the running server using:
mongod --shutdown
Then run:
sudo mongod
After running this commands below:
sudo lsof -iTCP -sTCP:LISTEN -n -P
Search for mongod COMMAND and its PID and type,
sudo kill <mongo_command_pid>
And restarting mongod, it still fails with the same error "SocketException: Address already in use MONGODB".
Server was restarted yesterday and then this happened.
Just this single command
sudo pkill -f mongod
Make Sure the error is of the port , if using macos catalina there is a reported issue for ~/data/db which also causes this.
If your mongod process is already down, just remove the .sock file in /tmp:
My mongod is already down, so does not appears in:
lsof -iTCP -sTCP:LISTEN -n -P
The error was:
[root#ol7db19c1 ~]# mongod -f /etc/csrs_2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 6721
ERROR: child process failed, exited with error number 48
To see additional information in this output, start without the "--fork" option.
In the log file (/var/mongodb/db/csrs2.log):
2020-07-05T16:03:59.354-0300 E STORAGE [initandlisten] Failed to set up listener: SocketException: Address already in use
So, find for the socket locking:
ls -ltra /tmp|grep .sock
And just remove it (do it with caution in production environment):
rm -f mongodb-26002.sock
G-luck!
This may sound obvious but you should check if mongodb is already running.
So instead of first starting the the DB by running:
sudo mongod
Try directly running:
mongo

MongoDB - shutting down with code 48

I am trying to start MongoDB but the terminal returns the following error:
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] MongoDB starting : pid=25184 port=27017 dbpath=/data/db 64-bit host=Janiss-MacBook-Pro.local
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] db version v3.4.1
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] git version: 5e103c4f5583e2566a45d740225dc250baacfbd7
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2k 26 Jan 2017
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] allocator: system
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] modules: none
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] build environment:
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] distarch: x86_64
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] target_arch: x86_64
2017-02-06T16:26:27.037+0000 I CONTROL [initandlisten] options: {}
2017-02-06T16:26:27.038+0000 E NETWORK [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2017-02-06T16:26:27.038+0000 E NETWORK [initandlisten] addr already in use
2017-02-06T16:26:27.038+0000 E NETWORK [initandlisten] Failed to set up sockets during startup.
2017-02-06T16:26:27.038+0000 E STORAGE [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets
2017-02-06T16:26:27.038+0000 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-02-06T16:26:27.038+0000 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-02-06T16:26:27.039+0000 I CONTROL [initandlisten] now exiting
2017-02-06T16:26:27.039+0000 I CONTROL [initandlisten] shutting down with code:48
I am using Laravel Valet if that matters.
It seems like you have already a process running on the port where you want to start mongodb:
listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2017-02-06T16:26:27.038+0000 E NETWORK [initandlisten] addr already in use
you could try to kill the process that runs on that port with this command:
sudo kill sudo lsof -t -i:27017
or define another port for mongodb if you have another program using that port.
to run mongodb on a port other than its default port (27017) use the --port 27018 argument when starting mongodb from the terminal
If you are using mac then you can simply kill the process running on port 27017 which is mongodb process in most cases.
Simply run the command.
npx kill-port 27017
After that you can run the mongod command as you normally do.
Or if you are using windows follow these steps
Run command line as Admin
netstat -ano | findstr :27017
you will get pid in the end that is the process id to kill
taskkill /PID <typeyourPIDhere> /F
after successful termination of process you can run mongod as you usually do!
Enjoy!
This has worked for me
Initially i was facing various issues, like when i tried to start the server by using:
sudo mongod
then i changed port for mongodb
sudo mongod --port 27018
Now mongod server is running tentatively
sudo mongod
Basically you just need to force quit the mongo, you can do this just by typing
sudo pkill -f mongod
and now you can start the server again with
mongod
For me the kill process did not work. Probably because my mongo instance was running on AWS. So, what I did was log onto a mongo shell. (This was possible as mongo instance is running) And switched to the admin DB thru 'use admin' and did 'db.shutdownServer()'. And that shut down the server cleanly
use admin
db.shutdownServer()
I had the same issue (MongoDB failed to start with exit code 48) after a reboot, as it could not bind to the given IP.
It turned out, that the mongod systemctl unit file was configured to launch the mongod service after the network.target. I changed this to network-online.target and it seems to work.
1.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
1.a Either you change the port of mongod by specifying port
number
mongod --dbpath /System/Volumes/Data/data/db (path of db) —port 27018
1.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 <PID>
2.Starting mongo db using brew services run mongodb-community
3.Type mongod or mongod --dbpath /System/Volumes/Data/data/db(path)
How I solved my issue?
I don't know the actual reason for this error and its solution but I deleted mongodb database folders C:\data\db and restarted my PC.
2017-02-06T16:26:27.038+0000 E NETWORK [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017.
It means the server 27017 is already in use.
You can try mongod --dbpath /System/Volumes/Data/data/db --port 27018.
Or
You can first stop the mongod by sudo pkill -f mongod.
Then restart the server by mongod --dbpath /System/Volumes/Data/data/db
In some cases we may be unaware of the process id where we have initiated mongodb , and we are not able to track it, as we might have forgot to shut it down in such cases:
step 1: first of all try to know the process id where it is being established, and
to know that, type below code in terminal:
lsof -i | grep 27017
Once you type the above code, you will be able to see process id's using your mongodb server:
enter image description here
As you can see in the above picture, there you can find that "9663" is the process id where your mongodb server is active.
Step 2: Now we need to kill this process, by typing the command:
kill <pid>
In the above code pid stands for process id, as shown in the above image the process id looks similar to that of "9336".
Step 3: Now we have successfully killed the process and then we are good to start
the server of mongodb using the command:
mongod
Now you are good to go!

Mongo Shell couldn't connect to the local server

I'm starting MongoDB using the following command:
sumeet#sumeet-acer:~$ sudo service mongod start
I get a reply as:
mongod start/running, process 7209
sumeet#sumeet-acer:~$
But when I try to enter MongoDB shell by typing mongo I get the following error:
sumeet#sumeet-acer:~$ mongo
MongoDB shell version: 2.0.4
connecting to: test
Fri Jun 12 14:01:59 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
By default, mongod and mongos use the 27017 port. Without any option, the client will try to connect to a server running on localhost and listening at that port.
A few general advices here:
check if the mongod server is really running (ps -edf | grep mongod);
check if it uses the default port (look in the config file1, or use nmap localhost -p0-65535 or netstat | grep 27017);
if mongod is not running, check in the log2 for clues about what goes wrong.
1 /etc/mongodb.conf (on Debian-like systems at least)
2 /var/log/mongodb (on Debian-like systems at least)
The default IP and Port for MongoDB is 127.0.0.1 & 27017.
If you are using config file for specifying settings than re-verify IP & Port mentioned in it.
If you are going with default option than mongod should function as expected.
try mongo 127.0.0.1:27017
this worked for me
For MongoDB 2.2.2 running on Ubuntu 12.10, it's in /var/lib/mongodb/mongod.lock
and after that running the repair command
sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/
courtesy: https://stackoverflow.com/a/13700262/4907105

Unable to connect to MongoDB

I've just installed MongoDB (standard Ubuntu build, not the latest stable) and for some reason I can't connect:
Mon Feb 6 03:11:22 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:79
exception: connect failed
Now my machine isn't 127.x.x.1 it's for some reason x.x.x.2 (But i've changed my config to bind to that address, and also tried to bind to my public IP which provided no avail.
Config:
#mongodb.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
#where to log
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 199.21.114.XX
port = 27017
I've checked the logs and only startup info is displaying in there.
I've also checked that the deamon is running too, and it is - I even tried running it manually with a --fork.
Has anyone come across this before? Or any suggestions?
Thx
UPDATE:
After a restart - here are the logs:
** WARNING: You are running in OpenVZ. This is known to be broken!!!
Tue Feb 7 19:43:32 [initandlisten] db version v1.8.2, pdfile version 4.5
Tue Feb 7 19:43:32 [initandlisten] git version: nogitversion
Tue Feb 7 19:43:32 [initandlisten] build sys info: Linux allspice 2.6.24-28-se$
Tue Feb 7 19:43:32 [initandlisten] *** warning: spider monkey build without ut$
Tue Feb 7 19:43:32 [initandlisten] waiting for connections on port 27017
Tue Feb 7 19:43:32 [websvr] web admin interface listening on port 28017
I solved this problem by commenting out the following line in my /etc/mongod.conf file.
# Listen to local interface only. Comment out to listen on all interfaces.
#bind_ip = 127.0.0.1
You will need to follow this config change with restarting the mongod process.
sudo service mongod restart
You must specify the IP (199.21.114.XX), by running mongo 199.21.114.XX.
Otherwise, if you want it to listen on localhost as well, you should remove the bind_ip setting in your config file.
Update Check your firewall config. Since you're connecting to the external IP, it can be configured to block, even from the local box.
sudo rm /var/lib/mongodb/mongod.lock
I had the same issue just because the silly mistake.
first remove mongodb file which is fine
rm -rf /tmp/mongodb-27017.sock
where I did mistake inside the /etc/mongod.conf
# network interfaces
net:
port: 27017
#bindIp: 127.0.0.1
bindIp: privateIp
instead of
net:
port: 27017
bindIp: 10.1.2.4
for listen to all available ips
bindIp: [127.0.0.1,10.128.0.2]
restart the mongodb
sudo service mongod start
hopefully this answer help for someone
in /etc/mongod.conf
I changed,
bindIp: 127.0.0.1
to
bindIp: 0.0.0.0
which is all ports. Yeah, I know it's risky, but I am running on an internal network.
BTW commenting out #bindIp: 127.0.0.1 did NOT work for me, running under Ubuntu 18.
By default, running the mongo console command will look for mongodb on your localhost, hence the 127.0.0.1 IP which is your local loopback. Also, the default config for mongod should be available on localhost. Its better to do it this way for now to start, unless you have turned on auth. Make sure you have auth enabled or your database is running openly on your public ip.
When you want to connect to a host other than localhost or your default config, you do:
mongo <host>
following steps may help you :
open terminal and run "sudo rm /var/lib/mongodb/mongod.lock"
now run " sudo mongod --repair "
now "sudo start mongodb"
now just check the status of mongodb status "sudo status mongodb"
5 lastly just invoke command "mongo"
I bumped into this when mongod was running but not yet running "properly". I'm guessing there is probably a better way to wait for it to come up after installation, but adding a sleep 10 seems to fix it.
A slightly less cavemanly approach might be to tail -f its log file and look for the event where it tells you that it is listening on the interface you specified.
local log=/var/log/mongodb/mongod.log
sudo -u mongodb touch "$log"
service mongod start &
launcher=$!
tail -0f "$log" |
grep -q 'port: 27017'
wait "$launcher"
sleep 1
The final sleep is a bit of an act of desperation; it seems to take a jiffy after it logs the startup until it's properly up and listening.
See also Bash shell script not getting connect to MongoDB even if status is active
Another way is to add mongod option param bind_ip
mongod -–help
--bind_ip arg comma separated list of ip addresses to
listen on - localhost by default
--bind_ip_all bind to all ip addresses
mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db --bind_ip 0.0.0.0
If you are using windows, run the services
then check mongoDB server is running, if not running, Start it.
mongoDB server by default run on localhost:27017.
Click localhost:27017 the create an admin user first.

Trouble connecting to Mongo Config Server from mongos

Folks,
I am setting up a MongoDB Cluster. The problem is when I am starting up 'mongos' it fails to connect to Mongod Config Server. If I start 'mongos' from localhost with same server as Mongod Config Server it connects & works.
localhost-$ mongos --port 30000 --configdb server-02:20000 [Works]
server-01-$ mongos --port 30000 --configdb server-02:20000 [Does Not Work]
Since from localhost I am able to start mongos by connecting to mongo config server & execute queries, it appears that config server is running fine.
Not able to understand what prevents servers to connect to one of their peers. When I attemped to connect first time from localhost, I got a prompt from OS asking whether process 'mongos' should be allowed to accept incoming connections for which I clicked allow. Perhaps if server fails silently because of this permission issue, with no way to prompt.
Error I recieve:
Sun May 8 01:14:46 ./mongos db version v1.8.1, pdfile version 4.5 starting (--help for usage)
Sun May 8 01:14:46 git version: a429cd4f535b2499cc4130b06ff7c26f41c00f04
Sun May 8 01:14:46 build sys info: Linux bs-linux64.10gen.cc 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41
uncaught exception in mongos main:
11002 socket exception [6] server [server-02:20000] mongos connectionpool error: couldn't connect to server server-02:20000
Well I did try clean install yet the problem persisted, so I moved my mongos to a different server with new install & it appears to connect to config server without any issue. Apparently there is some sort of socket contention issue with first server I tried to run mongos.
In all the directories of your mongodb's datas, you have a mongo.lock file. If this file is not empty, it seems to mean that one instance of mongodb executable (config, shard data server) is already running.
Just delete this file in each directory if not empty.
Check your bindip value, which is used by your mongo service. Take out the bindip flag. It works perfectly.