Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb: failed to connect to server on first connect

I get the following error:

Warning { MongoError: failed to connect to server [mongodb:27017] on first connect     at Pool.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/topologies/server.js:325:35)     at emitOne (events.js:96:13)     at Pool.emit (events.js:188:7)     at Connection.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/connection/pool.js:270:12)     at Connection.g (events.js:292:16)     at emitTwo (events.js:106:13)     at Connection.emit (events.js:191:7)     at Socket.<anonymous> (/Users/michaelks/Desktop/users/node_modules/mongodb-core/lib/connection/connection.js:173:49)     at Socket.g (events.js:292:16)     at emitOne (events.js:96:13)     at Socket.emit (events.js:188:7)     at connectErrorNT (net.js:1025:8)     at _combinedTickCallback (internal/process/next_tick.js:74:11)     at process._tickCallback (internal/process/next_tick.js:98:9)   name: 'MongoError',   message: 'failed to connect to server [mongodb:27017] on first connect' } 

Even though I get this in the terminal window where I run Mongo:

2016-12-25T03:45:23.715+0100 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58868 #8 (8 connections now open)  

It kinda looks like there is a connection..

I've tried both

$ mongod  

and

$ brew services start mongo  

This is my test_helper.js

const mongoose = require('mongoose'); mongoose.connect('mongodb:localhost/users_test'); mongoose.connection  .once('open', () => console.log('Good to go!'))  .on('error', (error) => {  console.warn('Warning', error);  }); 

I have not specifically made the db "users_test" as I am under the impression that mongoose or mongo or both will do this on the fly.

I've tried both "localhost" and "127.0.0.1​". I'm on OSX 10.11.5 I'm running Node 7.3.0 and Mongo 3.2.9

What am I doing wrong? How do I figure out what's wrong?

like image 411
Michael Krøyserth-Simsø Avatar asked Dec 25 '16 02:12

Michael Krøyserth-Simsø


People also ask

Why is my MongoDB not connecting?

Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

Can't connect to server connection refused MongoDB?

Normally this caused because you didn't start mongod process before you try starting mongo shell. This case worked out for me, i had to do ~>sudo mongod --dbpath /dbpathlocation and then opened the new terminal to run mongo...

How does MongoDB handle connection error?

In case of failing to connect to an mongodb server, your application should just notify you it's not the best approach to handle mongodb server start/restart from your application use daemons such as systemd or other process monitoring.


1 Answers

To connect to mongodb with mongoose, you can use :

mongoose.connect('mongodb://localhost/users_test'); 

or

mongoose.connect('localhost/users_test'); 

or

mongoose.connect('localhost','users_test'); 

But not mongoose.connect('mongodb:localhost/users_test');, it doesnt match the right hostname (mongodb instead of localhost)

like image 97
Bertrand Martel Avatar answered Sep 23 '22 08:09

Bertrand Martel