使用 ssh 隧道连接到 mysql 无效

Connecting to mysql using ssh tunnel does not work

我正在尝试通过 ssh 隧道连接到远程服务器,然后连接到 mysql。

我第一次尝试 mysql workbench 并且效果很好。 "fake" 信息如下所示: 使用相同的信息,我尝试使用 nodeJS 连接到 mysql,但我不知道该怎么做。请帮忙。这是我到目前为止尝试过的。

var mysql = require('mysql');
var tunnel = require('tunnel-ssh');

var config = {
  username:'hoffnung8493',
  Password:'password123',
  host:'hoffnung.com',// not sure if its right
  port:22,// not sure if its right
  dstHost:'hoffnung.com',// not sure if its right
  dstPort:3306, // not sure if its right
  localHost:'127.0.0.1',// not sure if its right
  localPort: 27000 // not sure if its right
};

tunnel(config, function(error, server){
  var db = mysql.createConnection({
    host: '127.0.0.1',// not sure if its right
    user: 'stock_id',
    password: 'stock_password',
    database: 'database_name',
    charset  : 'utf8'
  });
db.connect();
db.query(`show tables`, function(error, result){
  console.log(error);
  console.log(1, result);
  })
})

module.exports = db; //probably wrong

然后从 main.js 我想发送这样的查询:

db = require('./lib/db.js');

db.query('show tables', function(error, result){
    console.log(result)
});

在我的控制台上,我收到以下错误消息:

0|main   | Error: All configured authentication methods failed
0|main   |     at tryNextAuth (/home/cabox/workspace/node_modules/tunnel-ssh/node_modules/ssh2/lib/client.js:376:17)
0|main   |     at SSH2Stream.onUSERAUTH_FAILURE (/home/cabox/workspace/node_modules/tunnel-ssh/node_modules/ssh2/lib/client.js:587:5)
0|main   |     at emitTwo (events.js:125:13)
0|main   |     at SSH2Stream.emit (events.js:213:7)
0|main   |     at parsePacket (/home/cabox/workspace/node_modules/ssh2-streams/lib/ssh.js:3929:10)
0|main   |     at SSH2Stream._transform (/home/cabox/workspace/node_modules/ssh2-streams/lib/ssh.js:669:13)
0|main   |     at SSH2Stream.Transform._read (_stream_transform.js:186:10)
0|main   |     at SSH2Stream._read (/home/cabox/workspace/node_modules/ssh2-streams/lib/ssh.js:251:15)
0|main   |     at SSH2Stream.Transform._write (_stream_transform.js:174:12)
0|main   |     at doWrite (_stream_writable.js:385:12)

在我从 "Password" ->"password" 修复后,我在 db.connect 之后添加了 db.query 以查看它是否有效并且我得到以下错误

0|main   | { Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'stock'@'firstpg-server197-22' (using password: YES)
0|main   |     at Handshake.Sequence._packetToError (/home/cabox/workspace/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
0|main   |     at Handshake.ErrorPacket (/home/cabox/workspace/node_modules/mysql/lib/protocol/sequences/Handshake.js:124:18)
0|main   |     at Protocol._parsePacket (/home/cabox/workspace/node_modules/mysql/lib/protocol/Protocol.js:278:23)
0|main   |     at Parser.write (/home/cabox/workspace/node_modules/mysql/lib/protocol/Parser.js:76:12)
0|main   |     at Protocol.write (/home/cabox/workspace/node_modules/mysql/lib/protocol/Protocol.js:38:16)
0|main   |     at Socket.<anonymous> (/home/cabox/workspace/node_modules/mysql/lib/Connection.js:91:28)
0|main   |     at Socket.<anonymous> (/home/cabox/workspace/node_modules/mysql/lib/Connection.js:502:10)
0|main   |     at emitOne (events.js:115:13)
0|main   |     at Socket.emit (events.js:210:7)
0|main   |     at addChunk (_stream_readable.js:266:12)
0|main   |     --------------------
0|main   |     at Protocol._enqueue (/home/cabox/workspace/node_modules/mysql/lib/protocol/Protocol.js:144:48)
0|main   |     at Protocol.handshake (/home/cabox/workspace/node_modules/mysql/lib/protocol/Protocol.js:51:23)
0|main   |     at Connection.connect (/home/cabox/workspace/node_modules/mysql/lib/Connection.js:118:18)
0|main   |     at /home/cabox/workspace/inven_man/lib/db2.js:24:4
0|main   |     at Server.<anonymous> (/home/cabox/workspace/node_modules/tunnel-ssh/index.js:89:13)
0|main   |     at Object.onceWrapper (events.js:314:30)
0|main   |     at emitNone (events.js:105:13)
0|main   |     at Server.emit (events.js:207:7)
0|main   |     at emitListeningNT (net.js:1349:10)
0|main   |     at _combinedTickCallback (internal/process/next_tick.js:135:11)
0|main   |   code: 'ER_ACCESS_DENIED_ERROR',
0|main   |   errno: 1045,
0|main   |   sqlMessage: 'Access denied for user \'stock\'@\'firstpg-server197-22\' (using password: YES)',
0|main   |   sqlState: '28000',
0|main   |   fatal: true }
0|main   | 1 undefined

我现在无法测试,但将选项中的 port 设置为 27000 应该就足够了。

tunnel(config, function(error, server){
  var db = mysql.createConnection({
    host: '127.0.0.1',
    port: 27000, // the local port you defined for your tunnel
    user: 'stock_id',
    password: 'stock_password',
    database: 'database_name',
    charset  : 'utf8'
  });
  db.connect();
})

除此之外选项区分大小写,所以它应该是 password 而不是 Password

var config = {
  username:'hoffnung8493',
  password:'password123',
  host:'hoffnung.com',// not sure if its right
  port:22,// not sure if its right
  dstHost:'hoffnung.com',// not sure if its right
  dstPort:3306, // not sure if its right
  localHost:'127.0.0.1',// not sure if its right
  localPort: 27000 // not sure if its right
};

首先,我建议您以后更改/隐藏域名,以防万一。
其次,你能post你从控制台得到的输出吗?如果你得到任何输出。
第三,因为这是一个 ssh 连接问题,你需要确保你被设置为允许连接到远程服务器,在这种情况下是在端口 22 上,无论是在它的设置/防火墙设置中。

关于设置,仔细检查变量值以确保不要混淆 MySQL 和 SSH 连接变量,依赖于 MySQL Workbench 连接的事实正在工作。