1

I need to query a mysql database from one server. The mysql database is located on another network. I can only connect to this network from a specific server.

I need to create a tunnel from server 1, into server 2 and use this tunnel to connect to my mysql database.

This is my current code. This allows me to connect to server 2's local mysql server, but not to a remote mysql server.

var Client = require('ssh2').Client,
mysql = require('mysql2'),

var client = new Client();
var ssh = client.connect({
  host: config.server_tunnel.dstHost,
  port: 22,
  username: config.server_tunnel.username,
  privateKey: config.server_tunnel.privateKey
});

ssh.forwardOut('127.0.0.1', 12345, '127.0.0.1', 3306, function (err, stream) {
   mysql.createConnection({
    user: config.database.username,
    password: config.database.password,
    database: config.database.database,
    stream: stream,
    host: 'remote.host.com'
  });
});
CharliePrynn
  • 3,034
  • 5
  • 40
  • 68

1 Answers1

1

Instead of passing host to mysql.createConnection(), try making the tunnel connect to it:

ssh.forwardOut('127.0.0.1', 12345, 'remote.host.com', 3306, ...);
robertklep
  • 198,204
  • 35
  • 394
  • 381