22

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.

ignite-me
  • 738
  • 3
  • 14
  • 33

10 Answers10

18

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

matyas
  • 2,696
  • 23
  • 29
  • Killing the process worked, thanks! But now it returns a different error: `Assertion: 28595:2: No such file or directory src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 267 2017-02-06T16:36:37.075+0000 I STORAGE [initandlisten] exception in initAndListen: 28595 2: No such file or directory, terminating` – ignite-me Feb 06 '17 at 16:37
  • Maybe your installation of mongodb is compromized for some reason. You could try to download mongodb as standalone version here: https://www.mongodb.com/download-center extract this archive and run the mongod file that is located in the bin folder of this archive. If mongodb starts without problems from there you may have to reinstall mongodb on your system or use this version of mongodb that you just downloaded – matyas Feb 06 '17 at 16:41
  • to run the extracted archive of mongodb use this command: bin/mongod --dbpath /path to your db folder/ --port 5900' – matyas Feb 06 '17 at 16:43
15

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!

Vivek Maru
  • 8,347
  • 1
  • 24
  • 34
4

This has worked for me

Initially i was facing various issues, like when i tried to start the server by using: sudo mongod I received this error: shutting down with code 48

then i changed port for mongodb

sudo mongod --port 27018

Now mongod server is running tentatively

sudo mongod
Akash Jain
  • 483
  • 5
  • 5
4

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
  • Just a suggestion, It would also be helpful to understand the reason for the issue which user is experiencing, although, your answer may correct. – Rajeesh Madambat May 14 '20 at 05:52
3

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()
thor
  • 21,418
  • 31
  • 87
  • 173
MSi
  • 97
  • 3
1

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

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)

Harshit Ruwali
  • 1,040
  • 2
  • 10
  • 22
0

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.

WasiF
  • 26,101
  • 16
  • 120
  • 128
0

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.

  1. You can try mongod --dbpath /System/Volumes/Data/data/db --port 27018.

Or

  1. You can first stop the mongod by sudo pkill -f mongod. Then restart the server by mongod --dbpath /System/Volumes/Data/data/db
0

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!