0

I am not able to start mongo db. It gives the same error everytime.

2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] MongoDB starting : pid=80283 port=27017 dbpath=/data/db 64-bit host=ubuntu
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] db version v3.4.6
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] allocator: tcmalloc
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] modules: none
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] build environment:
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten]     distmod: ubuntu1604
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten]     distarch: x86_64
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten]     target_arch: x86_64
2017-07-19T07:39:16.964-0700 I CONTROL  [initandlisten] options: {}
2017-07-19T07:39:16.964-0700 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to lock file: /data/db/mongod.lock Resource temporarily unavailable. Is a mongod instance already running?, terminating
2017-07-19T07:39:16.964-0700 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-07-19T07:39:16.964-0700 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-07-19T07:39:16.965-0700 I CONTROL  [initandlisten] now exiting
2017-07-19T07:39:16.965-0700 I CONTROL  [initandlisten] shutting down with code:100

Or sometimes port in use error.

fogserver@iotlab-FogServer:~$ mongod
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] MongoDB starting : pid=21462 port=27017 dbpath=/data/db 64-bit host=iotlab-FogServer
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] db version v3.4.6
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] allocator: tcmalloc
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] modules: none
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] build environment:
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten]     distmod: ubuntu1604
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten]     distarch: x86_64
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten]     target_arch: x86_64
2017-07-19T17:24:01.299+0200 I CONTROL  [initandlisten] options: {}
2017-07-19T17:24:01.317+0200 E NETWORK  [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2017-07-19T17:24:01.317+0200 E NETWORK  [initandlisten]   addr already in use
2017-07-19T17:24:01.317+0200 E NETWORK  [initandlisten] Failed to set up sockets during startup.
2017-07-19T17:24:01.317+0200 E STORAGE  [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets
2017-07-19T17:24:01.317+0200 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-07-19T17:24:01.317+0200 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-07-19T17:24:01.317+0200 I CONTROL  [initandlisten] now exiting
2017-07-19T17:24:01.317+0200 I CONTROL  [initandlisten] shutting down with code:48
fogserver@iotlab-FogServer:~$

Then I have to restart the service everytime. Please suggest how this can be avoided.

0

This is happening because a MongoDB server is already running on your system, likely as a daemonized service.

According to your tags, you're running Ubuntu 16.04, which uses systemd. You can check the running state of the service using this command:

sudo systemctl status mongodb

If this reports as running, you already have a MongoDB server running - try to connect to it normally without manually starting mongod.

If you don't want/need a daemonized service to start at system boot, you can disable MongoDB from running with this:

sudo systemctl disable mongodb
sudo systemctl stop mongodb

Now, you can start and manually stop MongoDB using either mongod or by starting/stopping the daemon:

sudo systemctl start mongodb

I would personally highly recommend you continue to use your daemonized service, as you don't need to worry about it failing or similar. If security is a concern, block MongoDB's ports in your firewall.


If you need log entries for a running daemonized service, you can use the journalctl command, like so:

sudo journalctl -u mongodb

If you want a live journal, add the -f command-line flag to the journalctl command.


If, for some reason, you need both a daemonized MongoDB and a launch-on-demand MongoDB, you will need to change the bind information and data directory for one of the two instances. See here for part of the puzzle.

Alternatively, you can just stop the service while running the daemon directly.

  • Thanks for your reply. I followed the steps and got mongodb active but still not able to run it. database@ubuntu:~$ mongod MongoDB starting : pid=7058 port=27017 dbpath=/data/db 64-bit host=ubuntu I CONTROL [initandlisten] db version v3.4.6 NETWORK [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017 E NETWORK [initandlisten] addr already in use E NETWORK [initandlisten] Failed to set up sockets during startup. 2017-07-19T18:33:23.984-0700 E STORAGE [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets – Divya Nair Jul 20 '17 at 1:35
  • @DivyaNair What's your service status? According to that error, MongoDB is currently running. – Kaz Wolfe Jul 20 '17 at 3:08
  • Yes, it is running but not starting when i type mongod or sudo mongod – Divya Nair Jul 20 '17 at 10:33
  • If it's running, you don't need to run mongod again. That's the server command. If you want a CLI, use just mongo. – Kaz Wolfe Jul 20 '17 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.