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'
  });
});

1 Answer 1

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, ...);
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that makes total sense now you've pointed it out. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.