Cannot connect to remote db using ssh tunnel and activerecord

后端 未结 2 1576
小鲜肉
小鲜肉 2020-12-24 11:08

I\'m having some trouble with the following script:

require \'rubygems\'
require \'active_record\'
require \'net/ssh/gateway\'

gateway = Net::SSH::Gateway.n         


        
2条回答
  •  粉色の甜心
    2020-12-24 12:01

    I think the comment is right - the DB is conflicting with the event loop in the ssh code.

    Try this:

      require 'rubygems'
      require 'active_record'
      require 'net/ssh/gateway'
    
      gateway = Net::SSH::Gateway.new('myserver.com', 'myuser', :password => "mypass")
      puts "true" if gateway.active?
      port = gateway.open('127.0.0.1', 3306, 3307)
      fork.do
        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
      end  
    
      Process.wait
    
      gateway.shutdown!
    

提交回复
热议问题