Heroku + mongoose connection error: no primary server found in set

heroku, mongodb, mongoose, node.js, replication

Solution

This may be a URI problem. The format for DB URIs is:

mongodb://<user>:<pass>@host:port,host:port,...,host:port/db_name

That means that

mongodb://username:password@someid-a0.mongolab.com:39897/pm_prod,mongodb://someid-a1.mongolab.com:39897

should be:

mongodb://username:password@someid-a0.mongolab.com:39897,someid-a1.mongolab.com:39897/pm_prod

It's also worth noting for others: If you are using Heroku's MongoLab add-on, your URI is available as an environment variable at process.env.MONGOLAB_URI, so you don't have to place the URI in your code.

This could also be related to a running question about connectivity between Heroku and Mongo. See https://github.com/jcottr/temps-mort, which references two tickets for the Node Mongo driver:

https://jira.mongodb.org/browse/NODE-4

https://jira.mongodb.org/browse/NODE-5

Problem

I have a mongodb replica set on mongolab. I'm using nodejs + mongoose. When I'm trying to connect from my local machine everything goes ok. But after deployment to heroku something wrong happens and mongoose got strange error: ``` [Error: no primary server found in set] ``` Here some code (server.js): ``` async.series([ function(callback){ console.log('DB Connection: ' + siteConf.mongo_url); mongoose.connect(siteConf.mongo_url, siteConf.mongo_options, callback); }, function(callback){ http.createServer(app).listen(siteConf.port, callback); } ], function(err, results){ if (err) { console.log(err); } console.log('Running in ' + (process.env.NODE_ENV || 'development') + ' mode @ ' + siteConf.uri); } ); ``` This url I'm using as connection string: ``` mongodb://username:password@someid-a0.mongolab.com:39897/pm_prod,mongodb://someid-a1.mongolab.com:39897 ``` The main thing I can't understand is: what is the differences between my maching and heroku cloud hosting. I already tried to remove node_modules and npm install them to be sure that I have same versions as on heroku. (Because heroku do this on each deploy). Thanks, and sorry for my bad english

Original source