22

So I'm working my way though the Getting Mean book from Manning and following the steps in Chapter 5 I'm trying to use a db on Mongolab as an add-on to Heroku. When I run this code (both locally and on Heroku) it returns this error:

MongoError: getaddrinfo ENOTFOUND undefined undefined:27017

This is my current code:

var mongoose = require('mongoose');

var dbURI = "mongodb://localhost/loc8r";
if (process.env.NODE_ENV === 'production') {
    dbURI = process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);

Troubleshooting I started the app from the terminal with:

NODE_ENV=production MONGOLAB_URI=/*my mongo uri here*/

with that it runs fine locally. So I'm not sure where the issue is coming from. Any suggestions for troubleshooting the error listed?

blipblop
  • 347
  • 1
  • 2
  • 14

9 Answers9

28

I think you are not providing the PORT NO. required for mongoDB.

Please give the port no.(27017) along with localhost.

Try this:

var dbURI = "mongodb://127.0.0.1:27017/loc8r";

getaddrinfo ENOTFOUND means client was not able to connect to the given address. Please try with the above address.

I hope this helps.

Rico
  • 58,485
  • 12
  • 111
  • 141
Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52
7

if you use docker with this problem, I solve that with this solution

I have the same problem, other solutions not work for me but i did that with this way

for mongo URI you must use your mongodb service name instead 127.0.0.1 or localhost

for example in below docker-compose file my mongo service name is mongodb-myapp and i change uri like this mongodb://mongodb-myapp:27017/myapp and it works for me


services:
  boilerplate-api-app:
    build: .
    environment:
      - MONGO_URI=mongodb://mongodb-myapp:27017/myapp
    volumes:
      - .:/app
    ports:
      - "5000:5000"
    depends_on:
      - mongodb-myapp

 mongodb-myapp:
    image: mongo
    ports:
      - "27017:27017"
hamidreza nikoonia
  • 2,007
  • 20
  • 28
6

You just missed specifying the port number as shown:

  var dbURI = "mongodb://localhost:27017/thenDBname"

Make sure to change localhost while hosting on production server.

Stunner
  • 12,025
  • 12
  • 86
  • 145
Itguymax
  • 81
  • 1
  • 2
2

The Heroku environment variable that gets created for an mLab add-on is called MONGODB_URI (MONGOLAB_URI may be a legacy thing).

pneumee
  • 494
  • 3
  • 8
  • I tried changing to `MONGODB_URI` but its still outputting the same error – blipblop Aug 23 '16 at 20:00
  • What is `dbURI` set to when mongoose tries to connect? (obviously mask or change any sensitive info in the connection string - the format is what I'm interested in seeing). – pneumee Aug 23 '16 at 20:06
  • Hmm I didn't think to check that... its coming in as `undefined`. This definitely has to be the issue, but looking at other people's setups I don't know why `process.env.MONGOLAB_URI` isn't returning anything in the above code – blipblop Aug 23 '16 at 20:28
  • Are you able to view the value via the Heroku CLI tool: https://devcenter.heroku.com/articles/mongolab#getting-your-connection-uri ? – pneumee Aug 23 '16 at 20:31
  • yes with that command I get something that looks like: mongodb://username:pasword@ds010000.mlab.com:10000/heroku_adcljdgt – blipblop Aug 23 '16 at 20:38
  • Cool. And you're certain that the code is using `process.env.MONGODB_URI` and not `process.env.MONGOLAB_URI` (which would be undefined)? – pneumee Aug 23 '16 at 22:28
2

So I'm not sure what part of this process fixed the issue but I completely removed/deleted the mongolab addon from Heroku. Then I added it back on and performed the same exact steps with the same code and it worked!

Thanks for everyone who was helping out!

blipblop
  • 347
  • 1
  • 2
  • 14
1

The Heroku environment variable that gets created for an mLab add-on is called MONGODB_URI (MONGOLAB_URI may be a legacy thing). (pneumee)

This solved the problem for me.

Max Tamtam
  • 41
  • 1
0

This looks correct. I've seen this error (a lot) before as well. The 'undefined undefined' I believe is referring to your environment variables being undefined. Try console logging your environment variables to make sure they're valid.

Adam
  • 877
  • 2
  • 10
  • 24
  • so i console logged my environment and pushed it to heroku and it outputs 'production' which seems good. so I'm guessing the error is something to do with using `process.env.MONGOLAB_URI` for some reason – blipblop Aug 23 '16 at 19:28
  • Looking at the conversation below, pneumee is completely right about MONGOLAB_URI being legacy. I forgot about that, MONGODB_URI is what you'll want to use as your Heroku env variable. However, for local testing, that shouldn't matter since you're just creating and providing a variable for yourself. Are you using Nodemon to start your application? What is the exact command you're using to start up? For debugging, also try adding console.log('DB URI: ' + process.env.MONGOLAB_URI); somewhere in this file just so you can easily spy on it. – Adam Aug 23 '16 at 21:36
0

I had the same error, however, in my case I used different docker containers. Basically, the error is telling you that your application can not connect to the monogodb server. This can arise for instance by false port number, false IP address, etc. In my case the docker I had mongodb on was not in a same network as my server and so, the server couldn't fetch data from database.

To solve/debug it I done the followings:

  1. Get the list of docker networks:
docker network ls
  1. Inspect each network and find corresponding containers:
docker network inspect [Network Name]
  1. Containers which must communicate with each other must be in a same network. So in my case I just disconnected server container from its network and reconnect it to the mongodb-network:
docker network disconnect [Network Name] [Container Name] 
docker network connect [Network Name] [Container Name]    
Keivan
  • 1,300
  • 1
  • 16
  • 29
0

If you're getting this while running some replica sets in Docker Compose and trying to connect MongoDB Compass just make sure that you have the 'Direct connection' checked.

enter image description here

David
  • 2,109
  • 1
  • 22
  • 27