title: Error accessing remote DB with sequelize via tunnel-ssh
body: <p>I have an EC2 Ubuntu instance on AWS with MySQL DB which I can access with the following configuration via HeidiSQL and everything works fine. My main goal is to connect to this DB through <code>sequelize</code>. I have tried to do it with npm package <code>tunnel-ssh</code> but constantly getting errors.</p>
  
  <p>Settings</p>
  
  <p><a href="https://i.stack.imgur.com/IdsWx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IdsWx.jpg" alt="settings"></a></p>
  
  <p>SSH Tunnel Settings</p>
  
  <p><a href="https://i.stack.imgur.com/Ym7Ro.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ym7Ro.jpg" alt="ssh tunnel settings"></a></p>
  
  <p>AWS EC2 Instance security group settings:</p>
  
  <p><a href="https://i.stack.imgur.com/nHvdF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nHvdF.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/ViDV2.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ViDV2.jpg" alt="enter image description here"></a></p>
  
  <p>Error message:</p>
  
  <pre><code>{ SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
      at Utils.Promise.tap.then.catch.err (G:\study\ssh-tunnel\sqlize-ssh\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:139:19)
      at tryCatcher (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\util.js:16:23)
      at Promise._settlePromiseFromHandler (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\promise.js:512:31)
      at Promise._settlePromise (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\promise.js:569:18)
      at Promise._settlePromise0 (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\promise.js:614:10)
      at Promise._settlePromises (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\promise.js:690:18)
      at _drainQueueStep (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\async.js:138:12)
      at _drainQueue (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\async.js:131:9)
      at Async._drainQueues (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\async.js:147:5)
      at Immediate.Async.drainQueues [as _onImmediate] (G:\study\ssh-tunnel\sqlize-ssh\node_modules\bluebird\js\release\async.js:17:14)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)
    name: 'SequelizeConnectionRefusedError',
    parent:
     { Error: connect ECONNREFUSED 127.0.0.1:3306
         at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
       errno: 'ECONNREFUSED',
       code: 'ECONNREFUSED',
       syscall: 'connect',
       address: '127.0.0.1',
       port: 3306,
       fatal: true },
    original:
     { Error: connect ECONNREFUSED 127.0.0.1:3306
         at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
       errno: 'ECONNREFUSED',
       code: 'ECONNREFUSED',
       syscall: 'connect',
       address: '127.0.0.1',
       port: 3306,
       fatal: true } }
  </code></pre>
  
  <p>packages I use: <code>mysql2, sequelize, tunnel-ssh</code></p>
  
  <p>I think so far I was able to open the ssh-tunnel and run sequelize but with errors. This is the code I've written:</p>
  
  <pre><code>const fs = require('fs');
  const Sequelize = require("sequelize");
  const tunnel = require('tunnel-ssh');
  const ppk = fs.readFileSync('./sample.ppk', 'utf-8');
  
  const sequelize = new Sequelize('test', 'login', 'password', {
    host: 'localhost',
    port: 3306,
    dialect: "mysql",
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    }
  }); 
  
  const sshConfig = {
    user: 'ubuntu',
    host: 'sshServer',
    port: 22,
    dstHost: 'localhost',
    dstPort: 3306,
    localHost: 'localhost',
    localPort: 3307,
    privateKey: ppk
  }
  
  // initiate tunnel
  tunnel(sshConfig, (error, server) =&gt; {
    if(error) {
      console.error(error);
    } else {
      console.log('server:', server);
  
      sequelize
          .authenticate()
          .then((db) =&gt; {
              console.log('CONNECTION ESTABLISHED! ', db);
          })
          .catch((err) =&gt; {
              console.error('UNABLE TO ESTABLISH CONNECTION: ', err);
          })
    }
  })
  </code></pre>