在mysql工作台上,我使用连接方法Standard TCP/IP over SSH
连接到了远程数据库。但是我很难在node.js
中建立相同的连接。
我在MySQL工作台中使用的连接参数是:
SSH Hostname
,SSH Username
,SSH Password
,MySQL Hostname
,MySQL Server Port
,Username
。包括Password
在内的其他参数保留为空。
var mysql = require("mysql2");
var Client = require("ssh2").Client;
var ssh = new Client();
var db2 = new Promise(function(resolve, reject) {
ssh
.on("ready", function() {
ssh.forwardOut(
// source address, this can usually be any valid address
"127.0.0.1",
// source port, this can be any valid port number
12345,
// destination address (localhost here refers to the SSH server)
"127.0.0.1",
// destination port
3306,
function(err, stream) {
console.log(stream);
if (err) throw err; // SSH error: can also send error in promise ex. reject(err)
// use `sql` connection as usual
connection = mysql.createConnection({
host: "MySQL Hostname",
user: "username",
password: "",
database: "Default Schema",
stream: stream
});
// send connection back in variable depending on success or not
connnection.connect(function(err) {
if (err) {
resolve(connection);
} else {
reject(err);
}
});
}
);
})
.connect({
host: "SSH Hostname",
port: 22,
username: "SSH Username",
password: "SSH Password"
});
});
module.exports = db2;
以下是错误消息:
Error: getaddrinfo ENOTFOUND SSH Hostname SSH Hostname:22
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
Emitted 'error' event at:
at Socket.<anonymous> (C:\Users\Sihoon Kim\Desktop\development\inventory_management\node_modules\ssh2\lib\client.js:306:10)
at Socket.emit (events.js:189:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)