使用Mysql Db从Heroku SSH进入远程服务器 [英] SSH from Heroku into remote server with Mysql Db

查看:176
本文介绍了使用Mysql Db从Heroku SSH进入远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个Rails应用程序,它需要从Heroku建立远程SSH会话(SSH隧道?)到远程Mysql数据库,作为背景ActiveRecord会话的一部分。目标是通过这个渠道将不同时间的数据迁移到应用程序中。通过网络连接到远程mysql数据库不会是一个选项。



几乎没有问题:


  1. Heroku是否允许SSHing关闭他们的Dyno?

  2. 这样做的
    缺点是什么?

  3. 我需要关心SSH
    会话持久性(工作可能需要1小时)吗?

  4. 最后,如何配置Rails和Heroku来为
    database.yml mysql端点启用远程连接?


解决方案

我尝试了@ user1322092的方法,并在多个客户端试图访问数据库时遇到了麻烦,因为每个人都会尝试打开一个连接首先会得到一个错误)。



我创建了一个可怕的解决方案,包括在dyno上产生一个SSH进程并将其用于所有通信。血淋淋的细节在 https://stackoverflow.com/a/27361295/558639 。除了非常糟糕之外,无论您是否访问远程数据库,每次启动进程时都会导致延迟。



更新



所以这里有一个更好的方法,看起来效果不错:只是陷入(并忽略) Errno :: EADDRINUSE 错误。代码如下:

$ p $ require'net / ssh / gateway'

class Mole

TUNNEL_HOST =< redacted>
TUNNEL_USER =< redacted>
AWS_HOST =< redacted>
TUNNEL_PORT_NUMBER = 3307

attr_reader:tunnel_gateway,:tunnel_port

#调用此命令从当前机器打开SSH隧道。如果
#隧道已经被打开(例如被另一个进程打开),那么
#会收到警告消息
def open_tunnel
@tunnel_gateway = Net :: SSH :: Gateway.new(TUNNEL_HOST,TUNNEL_USER)
begin
@tunnel_port = @ tunnel_gateway.open(AWS_HOST,3306,TUNNEL_PORT_NUMBER)
rescue Errno :: EADDRINUSE => e
$ stderr.puts(Warning:#{e.class}:#{e.message})
结束
结束

# t通常会调用它,因为隧道是一个系统范围的
#资源;你不知道其他进程何时想要释放它。
def close_tunnel
r = @ tunnel_gateway.close(@tunnel_port)if @tunnel_gateway
@ tunnel_gateway.shutdown!
r
end

end


I'm looking at an Rails app that would require establishing a remote SSH session (SSH Tunnel?) from Heroku to a remote Mysql database as part of an background, ActiveRecord session. The goal would be to migrate data at different times via this channel into the app. Connecting to the remote mysql database over the web would not be an option.

Few questions:

  1. Does Heroku permit SSHing off their Dyno?
  2. What would be the downsides of doing so?
  3. Would I have to be concerned about SSH session persistence (job could take 1 hour)?
  4. Lastly, how can I configure Rails and Heroku to enable remote connections for a database.yml mysql endpoint?

解决方案

I tried @user1322092's approach and got into trouble when multiple clients tried to access the db, since each one would attempt to open a connection (all but the first would get an error).

I created a monstrous solution involving spawning an SSH process on the dyno and using that for all communications. Gory details are in https://stackoverflow.com/a/27361295/558639. Aside from being really kludgy, it incurs a delay every time a process starts up, whether or not you're accessing the remote database.

update

So here's a better approach that appears to work well: just trap (and ignore) Errno::EADDRINUSE errors. Here's the code:

require 'net/ssh/gateway'

class Mole

  TUNNEL_HOST = <redacted>
  TUNNEL_USER = <redacted>
  AWS_HOST = <redacted>
  TUNNEL_PORT_NUMBER = 3307

  attr_reader :tunnel_gateway, :tunnel_port

  # Call this to open an SSH tunnel from the current machine.  If the
  # the tunnel was already opened (e.g. by another process) , you'll
  # get a warning message
  def open_tunnel
    @tunnel_gateway = Net::SSH::Gateway.new(TUNNEL_HOST, TUNNEL_USER)
    begin
      @tunnel_port = @tunnel_gateway.open(AWS_HOST, 3306, TUNNEL_PORT_NUMBER)
    rescue Errno::EADDRINUSE => e
      $stderr.puts("Warning: #{e.class}: #{e.message}")
    end
  end

  # You won't normally call this, because the tunnel is a system wide
  # resource; you don't know when other processes want to release it.
  def close_tunnel
    r = @tunnel_gateway.close(@tunnel_port) if @tunnel_gateway
    @tunnel_gateway.shutdown!
    r
  end

end

这篇关于使用Mysql Db从Heroku SSH进入远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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