title: Node.js SSH Tunneling to MongoDB using Mongoose
body: <p>I have two Mongo DBs. One for my dev environment, one for production as seen here from my Robomongo setup:</p>
  
  <p><a href="https://i.stack.imgur.com/3ww9y.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3ww9y.png" alt="Two Mongo DBs"></a></p>
  
  <p>The production db is SSH tunneled to my Digital Ocean virtual server as seen here (specifics have been changed obviously). This setup works fine when connecting to/from my production website:</p>
  
  <p><a href="https://i.stack.imgur.com/om0HY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/om0HY.png" alt="SSH Tunnel Settings"></a></p>
  
  <p>I am now working on a different/related project and need to connect my <strong><em>local</em></strong> machine to my <strong><em>production</em></strong> DB, so I assumed I'd need to use something like <a href="https://www.npmjs.com/package/tunnel-ssh" rel="noreferrer">tunnel-ssh</a> to get it done. I've followed <a href="https://stackoverflow.com/a/38514734/2690558">this answer</a> as an example, but I'm either getting <code>Error: (SSH) Channel open failure: open failed</code> OR it's connecting to my Dev db (when I use 27017 as my dstHost/dstPort/localPort). I must be thinking about this wrong or I'm being dumb with my configuration. I am admittedly a total Mongo/Mongoose novice, so both are equally possible. Any advice?</p>
  
  <pre><code>var fs = require("fs");
  var mongoose = require('mongoose');
  var tunnel = require('tunnel-ssh');
  
  //===== db connection =====
  
  var config = {
      username:'my.username',
      host:'my.ip.address',
      agent : process.env.SSH_AUTH_SOCK,
      privateKey:require('fs').readFileSync('/Users/myusername/.ssh/id_rsa'),
      port:22,
      dstHost:'mongodb://localhost:27000/mydbname',
      dstPort:27000,
      localHost:'127.0.0.1',
      password:'mypassword',
      localPort: 27000
  };
  
  var server = tunnel(config, function (error, server) {
      if(error){
          console.log("SSH connection error: " + error);
      }
      mongoose.connect('mongodb://localhost:27000/mydbname');
  
      var db = mongoose.connection;
      db.on('error', console.error.bind(console, 'DB connection error:'));
      db.once('open', function() {
          // we're connected!
          console.log("DB connection successful");
          // console.log(server);
      });
  });
  </code></pre>
  
  <p>Thanks in advance!</p>