SocketException: Address already in use MONGODB
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
-
probably use default port
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)
- method 1:
- kill by PID:
kill -9 425
- find mongod PID
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 install community version in Mac by:
-
(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
- output can see
-
brew services list
- if installed by
brew
and start bybrew
- if installed by
-
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