Search code examples
javascriptnode.jsmongodb

Mongodb is running however still cannot connect


This is what I get when I run service mongod status:

    ● mongod.service - LSB: An object/document-oriented database
   Loaded: loaded (/etc/init.d/mongod; bad; vendor preset: enabled)
   Active: active (running) since Sun 2017-02-12 15:05:42 UTC; 4min 59s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 6984 ExecStop=/etc/init.d/mongod stop (code=exited, status=0/SUCCESS)
  Process: 6998 ExecStart=/etc/init.d/mongod start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mongod.service
           └─7009 /usr/bin/mongod --config /etc/mongod.conf

Feb 12 15:05:41 WGJG008 systemd[1]: Stopped LSB: An object/document-oriented database.
Feb 12 15:05:41 WGJG008 systemd[1]: Starting LSB: An object/document-oriented database...
Feb 12 15:05:41 WGJG008 mongod[6998]:  * Starting database mongod
Feb 12 15:05:42 WGJG008 mongod[6998]:    ...done.
Feb 12 15:05:42 WGJG008 systemd[1]: Started LSB: An object/document-oriented database.
Feb 12 15:08:25 WGJG008 systemd[1]: Started LSB: An object/document-oriented database.

However whenever I run my server

sudo node server.js

I get the following error message:

MongoError: failed to connect to server [soren_site:27017] on first connect
at null.<anonymous> (/var/www/soren_api/node_modules/mongodb-core/lib/topologies/server.js:325:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/var/www/soren_api/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/var/www/soren_api/node_modules/mongodb-core/lib/connection/connection.js:173:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:998:8)
at nextTickCallbackWith2Args (node.js:441:9)
at process._tickCallback (node.js:355:17)

I connect to my server in the following way:

mongoose.connect('mongodb://soren_site');

What am I doing wrong?

Output of mongod:

    2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] MongoDB starting : pid=7726 port=27017 dbpath=/data/db 64-bit host=WGJG008
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] db version v3.0.14
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] git version: 08352afcca24bfc145240a0fac9d28b978ab77f3
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] build info: Linux ip-10-30-223-232 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2017-02-12T15:29:21.219+0000 I CONTROL  [initandlisten] options: {}
2017-02-12T15:29:21.243+0000 E NETWORK  [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2017-02-12T15:29:21.243+0000 E NETWORK  [initandlisten]   addr already in use
2017-02-12T15:29:21.243+0000 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-02-12T15:29:21.244+0000 I CONTROL  [initandlisten] dbexit:  rc: 100

Solution

  • This looks like hostname resolving issue.

    Make sure the hostname you pass in connection string is resolvable.

    Also, if not found, you may need to add entry to your /etc/hosts

    Hope this helps