# 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