Created
April 19, 2016 08:04
-
-
Save gouf/c2dcab103e0fa23ed9138d1d3b5f9432 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Ref : [How to create a ssh tunnel in ruby and then connect to mysql server on the remote host](http://stackoverflow.com/questions/4103809/how-to-create-a-ssh-tunnel-in-ruby-and-then-connect-to-mysql-server-on-the-remot) | |
| require 'net/ssh/gateway' # gem install net-ssh-gateway | |
| require 'mysql2' | |
| host = '192.168.0.2' | |
| user = 'root' | |
| pass = 'root' | |
| db = 'db_name' | |
| port = 22 | |
| gateway = Net::SSH::Gateway.new( | |
| host, | |
| user, | |
| password: pass | |
| ) | |
| port = gateway.open( | |
| '127.0.0.1', 3306, 3307 | |
| ) | |
| client = Mysql2::Client.new( | |
| host: '127.0.0.1', | |
| username: user, | |
| password: pass, | |
| database: db, | |
| port: port | |
| ) | |
| sql =<<EOF | |
| SELECT * | |
| FROM dtb_customer | |
| LIMIT 1 | |
| EOF | |
| results = client.query(sql) | |
| results.each do |row| | |
| p row | |
| end | |
| client.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment