mongodb-logo

MongoDB service not starting due Address already in use for socket: 127.0.0.1:27017

Hello folks, Hope you are doing best. Here is one more technical article on MongoDB.
My one of the test MongoDB instance was not able to start due to below mentioned error, the error itself more self-explanatory, Address: 27017 is already used for socket.

I encourage all my fellow DBA’s to hit database logs first, here you will find everything about database problem you are facing. Once you understand the error completely, you’ll find less difficult to resolve it.

Error Logs:

[root@mongodb ~]# service mongod start
Starting mongod: [FAILED]
[root@mongodb ~]# service mongod restart
Stopping mongod: [ OK ]
Starting mongod: [FAILED]
2016-09-23T23:16:33.078-0700 I CONTROL [main] ***** SERVER RESTARTED *****
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] MongoDB starting : pid=2761 port=27017 dbpath=/var/lib/mongo 64-bit host=mongodb
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] db version v3.2.9
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] git version: 22ec9e93b40c85fc7cae7d56e7d6a02fd811088c
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] allocator: tcmalloc
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] modules: none
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] build environment:
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] distmod: rhel62
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] distarch: x86_64
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] target_arch: x86_64
2016-09-23T23:16:33.087-0700 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-09-23T23:16:33.109-0700 E NETWORK [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 127.0.0.1:27017
2016-09-23T23:16:33.109-0700 E NETWORK [initandlisten] addr already in use
2016-09-23T23:16:33.109-0700 E STORAGE [initandlisten] Failed to set up sockets during startup.
2016-09-23T23:16:33.109-0700 I CONTROL [initandlisten] dbexit: rc: 48

Attempt to stop MongoDB instance was successful, as below. But I didn’t find any sort of logs in a database log file. (i.e. /var/log/mongodb/mongod.log)

[root@mongodb ~]# service mongod stop
Stopping mongod: [ OK ]

Thanks to netstat linux command as below:

[root@mongodb ~]# netstat -tlnp | grep 27017
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 2007/mongod

I have Killed mongod service with the “kill” linux command:

[root@mongodb ~]# kill -9 2007

After mongod service killed, my instance started successfully:

[root@mongodb ~]# service mongod start
Starting mongod: [ OK ]

Database logs also positive:

2016-09-23T23:30:17.430-0700 I CONTROL [main] ***** SERVER RESTARTED *****
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] MongoDB starting : pid=2938 port=27017 dbpath=/var/lib/mongo 64-bit host=mongodb
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] db version v3.2.9
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] git version: 22ec9e93b40c85fc7cae7d56e7d6a02fd811088c
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] allocator: tcmalloc
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] modules: none
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] build environment:
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] distmod: rhel62
2016-09-23T23:30:17.439-0700 I CONTROL [initandlisten] distarch: x86_64
2016-09-23T23:30:17.440-0700 I CONTROL [initandlisten] target_arch: x86_64
2016-09-23T23:30:17.440-0700 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-09-23T23:30:17.460-0700 I - [initandlisten] Detected data files in /var/lib/mongo created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2016-09-23T23:30:17.460-0700 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,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),
2016-09-23T23:30:17.907-0700 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongo/diagnostic.data'
2016-09-23T23:30:17.908-0700 I NETWORK [initandlisten] waiting for connections on port 27017
2016-09-23T23:30:17.909-0700 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker

I can open new connection too:

2016-09-23T23:30:52.860-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58051 #1 (1 connection now open)

Your comments are highly appreciable in advanced, if any.
Stay Tune. 🙂