question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

connecting to remote server and creating sequelize instance

See original GitHub issue

I’m pretty new at ssh tunneling and the like, so pleas excuse my lack of proper terminology / understanding.

I’m attempting to use ssh2 to ssh into a remote server, so I can then create a local database connection to that remote host’s database, and insert data into the database.

Here’s what I’ve got so far:

var Client = require('ssh2').Client;

var conn = new Client();
conn.on('ready', function() {
    console.log('Client :: ready');
    conn.forwardOut(
        '127.0.0.1',
        24000,
        '127.0.0.1',
        3306,
        function (err, stream) {
            if (err) {
                throw err;
            }

            console.log(stream);

            var sequelize = new Sequelize(<DATABASE>, <USER>, <PASSWORD>, {
                host: 'localhost',
                dialect: 'mysql',
                pool: {
                    max: 5,
                    min: 0,
                    idle: 10000
                }
            });

            sequelize.query('SELECT * FROM `orders` LIMIT 1',
                {
                    type: sequelize.QueryTypes.SELECT
                }).then(function (results) {
                    console.log(results);
                });
        }
    )
}).connect({
    host: '<IP ADDRESS>',
    port: 22,
    username: <SSH USER>,
    privateKey: require('fs').readFileSync('/path/to/id_rsa')
});

Attempting to connect to sequelize in the code results in a CONNECTION refused error.

I’m just wondering what I’m doing wrong here. My lack of a really strong understanding of what’s going on here means it’s been difficult to search for answers, so I’m hoping posting this here will be a good start.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
mscdexcommented, Nov 4, 2015

forwardOut() just creates one outgoing connection.

You need to create a TCP server that listens on a local port and calls forwardOut() for every local connection and then pipes between the local socket and the forwarded connection.

For example:

var sshConnected = false;
var conn = new Client();

conn.on('ready', function() {
  sshConnected = true;
  var sequelize = new Sequelize(<DATABASE>, <USER>, <PASSWORD>, {
    host: 'localhost',
    dialect: 'mysql',
    pool: {
      max: 5,
      min: 0,
      idle: 10000
    }
  });
  sequelize.query('SELECT * FROM `orders` LIMIT 1',
    {
      type: sequelize.QueryTypes.SELECT
     }).then(function (results) {
       console.log(results);
   });
}).on('close', function() {
  sshConnected = false;
}).connect({
  // ...
});

net.createServer(function(sock) {
  sock.on('error', function() {});
  if (!sshConnected) return sock.end();
  conn.forwardOut(
    '127.0.0.1',
    sock.remotePort,
    '127.0.0.1',
    3306,
    function (err, stream) {
      if (err) return sock.end();
      stream.pipe(sock).pipe(stream);
    });
}).listen(3306);
1reaction
mscdexcommented, Nov 4, 2015

Well, information on TCP servers (or TCP in general) is available anywhere on google. For information specifically on net, you can read the node documentation here.

ssh2 does not listen on a port for forwarding connections like the way the OpenSSH client does when you use -L xx:nnnn:xx. So creating a TCP server in node and then creating a new SSH forwarded connection for each connection to the TCP server is needed. This way when Sequelize (or even non-node processes) connects to 3306 locally, it will automatically get piped to the MySQL server running on the SSH server.

Since these forwarded connections are being multiplexed over the same SSH connection, you may need to make sure that your pool size does not exceed the maximum number of channels permitted by the server. I think the default is ~10 or so, so I think your current config should work if the SSH server config hasn’t changed its default on this particular setting. Otherwise you could modify the TCP server connection handler to spawn a separate SSH connection for each database connection instead of multiplexing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sequelize remote database access - Stack Overflow
Look at the API docs for creating a new sequelize instance: ... { port: config.port, host: config.server, logging: console.log, ...
Read more >
Getting Started - Sequelize
To connect to the database, you must create a Sequelize instance. This can be done by either passing the connection parameters separately to ......
Read more >
How To Use Sequelize with Node.js and MySQL - DigitalOcean
To do that, first, you need to log in to your MySQL instance. ... Similarly, if you are using a remote server, make...
Read more >
Sequelize not connecting to remote server : r/node - Reddit
While trying to access from one of the servers with mysql -h host -u user -p , the connection can be established, while...
Read more >
MySQL Connection With Node.js Using Sequelize and Express
REST (Representational State Transfer) helps in determining how the API appears, while CRUD (Create, Read, Update, and Delete) is simply an acronym for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found