Nodejs - mysql2流连接和远程主机

时间:2017-10-05 13:50:52

标签: node.js node-mysql2

我需要从一台服务器查询mysql数据库。 mysql数据库位于另一个网络上。我只能从特定服务器连接到此网络。

我需要从服务器1创建隧道到服务器2并使用此隧道连接到我的mysql数据库。

这是我目前的代码。这允许我连接到服务器2的本地mysql服务器,但不能连接到远程mysql服务器。

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 个答案:

答案 0 :(得分:1)

不要将[void][reflection.assembly]::loadwithpartialname("System.Windows.Forms") $form = New-Object System.Windows.Forms.Form $form.Width = 500 $form.Height = 200 $form.formborderstyle = 3 $form.StartPosition = "CenterScreen" $button1 = New-Object System.Windows.Forms.Button $button1.Text = "Search" $button1.Height = 25 $button1.Width = 85 $button1.Top = 100 $button1.Left = 25 $button1_OnClick = { Get-EventLog -LogName Security -After $a2.Value.Date | Where-Object {$_.EventID -eq "4800" -or $_.EventID -eq "4801"} | Format-Table } $button1.add_Click($button1_OnClick) $form.Controls.Add($button1) $form.ShowDialog() 传递给host,而是尝试让隧道连接到它:

mysql.createConnection()