Unable to Connect to MongoDB in Node App


I’m trying to get my Node app (separate droplet) to connect to my mongoDB droplet.

I’ve found a npm called tunnel-ssh however am having trouble.

It says “DB connection successful”, however data is not coming back. And when I do a console.log(mongoose) it shows the host and host as null.

If I do console.log(mongoose), after the console.log(“DB connection successful”); then it shows me the host.

Anyhow, my code looks like:

var tunnel = require(‘tunnel-ssh’);

var config = { agent : ‘myuser’, host: ‘xxx:xxx:xxx:xxx’ agent : process.env.SSH_AUTH_SOCK, privateKey:require(‘fs’).readFileSync(‘id_rsa’), port:22, dstPort:27010, keepAlive: true };

var server = tunnel(config, function (error, server) {

    console.log("SSH connection error: " + error);


var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
    console.log("DB connection successful");


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.


I got it all working however i can’t remember exactly what I did however from what i briefly remember it was something around the port numbers. I think i had them mixed up. My final config var looks slightly different to what I have above - it’s currently like:

var config = {
      username : 'xxxxxxx',
      host: '',
      localPort: 2xxx0

I’d suggest try debugging around the 3 port numbers (port, dstPort, localPort).

Good luck!

I’m having the SAME issue. Everything works perfectly on cloud9, but I can’t get mongo to connect to the app. Everything in the api folder (all the nodejs files) sends a 400 error bad request - not sure it’s mongo as much as connecting to the non-public parts of the server (which connect to the db). But I don’t know how to fix it, either. Did you find a resolution?