使用net/ssh/gateway建立通往mysql的ssh隧道 [英] Using net/ssh/gateway to establish ssh tunnel to mysql

查看:162
本文介绍了使用net/ssh/gateway建立通往mysql的ssh隧道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过ssh建立到远程服务器的隧道,然后使用转发的端口访问MySQL.

I'm attempting to establish a tunnel to a remote server via ssh, and then use the forwarded port to access MySQL.

我目前正在这样使用

$gateway = Net::SSH::Gateway.new('target.server', 'user')

def with_gateway
  $gateway.open("target.server", 3306) do |port|
    yield port
  end
end

在我心目中会与此相似...

Which in my mind would be similar to this...

`ssh -L #{port}:localhost:3306 -N user@target.server`

然后当我尝试使用它并执行类似操作时.

with_gateway do |port|
  puts `mysql -u user -ppass -h 127.0.0.1 -P #{port} -e SHOW\ DATABASES\;`
end

它给了我这个错误消息.

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

我想念什么?

推荐答案

$gateway.open("target.server", 3306) do |port|

更等效于

ssh -L #{port}:target.server:3306 -N user@target.server

如果您的mysql服务器仅侦听127.0.0.1(或内部IP地址,或者防火墙仅允许通过内部网络进行连接,所有这些都是合理且正常的配置),则可能会失败.

which may very well fail if your mysql server only listens on 127.0.0.1 (or on an internal IP address, or firewalled to only allow connections via internal networks, all of which are reasonable and normal configurations).

可能想要:

$gateway.open("127.0.0.1", 3306) do |port|

在这种情况下.

这篇关于使用net/ssh/gateway建立通往mysql的ssh隧道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆