我正在尝试通过ssh隧道连接到远程服务器,然后连接到mysql。
我第一次在mysql工作台上试用,效果很好。“假”信息如下:
我用同样的信息试图用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
2条答案
按热度按时间cidc1ykv1#
首先,我建议您以后更改/隐藏域名,以防万一。
第二,你能发布你从控制台得到的输出吗?如果你得到任何输出。
第三,由于这是一个ssh连接问题,您需要确保您被设置为允许连接到远程服务器,在这种情况下,在端口22上,无论是在设置/防火墙设置中。
关于设置,请仔细检查变量值以确保不会混淆mysql和ssh连接变量,这取决于mysql workbench连接是否正常工作。
c0vxltue2#
我现在不能测试它,但它应该足以设置
port
在选项中27000
.除此之外,选项是区分大小写的,所以应该是
password
而不是Password
.