Guest User

Untitled

a guest
Sep 21st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. require 'rubygems'
  2. require 'active_record'
  3. require 'mysql2'
  4. require 'net/ssh/gateway'
  5.  
  6. gateway = Net::SSH::Gateway.new(
  7. 'remotehost.com',
  8. 'username'
  9. )
  10.  
  11. # opens a new port on the established gateway
  12. port = gateway.open('127.0.0.1', 3306, 3307)
  13.  
  14. # use cmd line to verify connection over ssh tunnel
  15. # mysql -u root -h 127.0.0.1 --port 3307
  16.  
  17. client = Mysql2::Client.new(
  18. host: "127.0.0.1",
  19. username: 'root',
  20. password: '',
  21. database: 'app_development',
  22. port: port
  23. )
  24. results = client.query("SELECT * FROM projects")
  25. results.each do |row|
  26. p row
  27. end
  28. client.close
  29.  
  30. gateway.shutdown!
  31.  
  32. class Company < ActiveRecord::Base
  33. establish_connection(
  34. :adapter => "mysql2",
  35. :host => "127.0.0.1",
  36. :username => "root",
  37. :password => "",
  38. :database => "app_development",
  39. :port => 3307 # have to specify the forwarded port for this example due to class scope
  40. )
  41. end
  42. puts Company.all.size
Add Comment
Please, Sign In to add comment