gpt4 book ai didi

mysql - 使用ssh隧道连接mysql不起作用

转载 作者:行者123 更新时间:2023-11-29 10:02:27 27 4
gpt4 key购买 nike

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

我首先在 mysql workbench 上尝试过,它工作得很好。 “假”信息如下所示: mysql connection info使用相同的信息,我尝试使用 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)

从“密码”->“密码”修复后,我在 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
};

关于mysql - 使用ssh隧道连接mysql不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52639438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com