无法使用 ssh 隧道和 activerecord 连接到远程数据库 [英] Cannot connect to remote db using ssh tunnel and activerecord
本文介绍了无法使用 ssh 隧道和 activerecord 连接到远程数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用以下脚本时遇到了一些问题:
I'm having some trouble with the following script:
require 'rubygems'
require 'active_record'
require 'net/ssh/gateway'
gateway = Net::SSH::Gateway.new('myserver.com', 'myuser', :password => "mypass")
puts "true" if gateway.active?
p = gateway.open('127.0.0.1', 3306, 3307)
class MyClass < ActiveRecord::Base
establish_connection(
:adapter => "mysql",
:host => "127.0.0.1",
:username => "db_user",
:password => "db_pass",
:database => "mydb_production",
:port => 3307
)
end
puts MyClass.all.size
gateway.shutdown!
当我运行脚本时,它只是挂起,除非我删除 activerecord 查询.我知道我可以使用隧道连接,因为我可以像这样从命令创建一个隧道:
When I run the script it just hangs, unless I remove the activerecord query. I know I can connect using tunneling because I can create a tunnel from the command like like so:
ssh -f myuser@myserver.com -L 3307/127.0.0.1/3306 -N
然后如果我跑:
require 'rubygems'
require 'active_record'
class MyClass < ActiveRecord::Base
establish_connection(
:adapter => "mysql",
:host => "127.0.0.1",
:username => "db_user",
:password => "db_pass",
:database => "mydb_production",
:port => 3307
)
end
puts MyClass.all.size
它工作正常.我做错了什么?
It works fine. What am I doing wrong?
谢谢.
推荐答案
通过使用 mysql2 gem,我能够在没有 fork 的情况下使其工作
I was able to get this to work without a fork by using the mysql2 gem
require 'rubygems'
require 'active_record'
require 'mysql2'
require 'net/ssh/gateway'
gateway = Net::SSH::Gateway.new(
'remotehost.com',
'username'
)
port = gateway.open('127.0.0.1', 3306, 3307)
class Company < ActiveRecord::Base
establish_connection(
:adapter => "mysql2",
:host => "127.0.0.1",
:username => "dbuser",
:password => "dbpass",
:database => "dbname",
:port => 3307
)
end
puts Company.all.size
这篇关于无法使用 ssh 隧道和 activerecord 连接到远程数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文