Skip to content

Instantly share code, notes, and snippets.

@btoone
Created March 28, 2012 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save btoone/2230396 to your computer and use it in GitHub Desktop.
Save btoone/2230396 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'active_record'
require 'mysql2'
require 'net/ssh/gateway'
gateway = Net::SSH::Gateway.new(
'remotehost.com',
'username'
)
# opens a new port on the established gateway
port = gateway.open('127.0.0.1', 3306, 3307)
# use cmd line to verify connection over ssh tunnel
# mysql -u root -h 127.0.0.1 --port 3307
client = Mysql2::Client.new(
host: "127.0.0.1",
username: 'root',
password: '',
database: 'app_development',
port: port
)
results = client.query("SELECT * FROM projects")
results.each do |row|
p row
end
client.close
gateway.shutdown!
class Company < ActiveRecord::Base
establish_connection(
:adapter => "mysql2",
:host => "127.0.0.1",
:username => "root",
:password => "",
:database => "app_development",
:port => 3307 # have to specify the forwarded port for this example due to class scope
)
end
puts Company.all.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment