DBI hangs while connecting to MySQL Over SSH TUnnel

Apologies, Even after doing extensive research, I could not find how to
format post on ruby-forum. So, please point me in right direction as
this is my 1st post on ruby-forum.

I am trying to connect to a MySQL DB Over SSH tunnel.

Here is my code:

require ‘net/ssh/gateway’
require ‘dbi’

puts “Creating SSH Tunnel …”
gateway = Net::SSH::Gateway.new(‘bastion_host’, ‘ubuntu’, :keys =>
gateway.open(‘MySQL_End_Point’, 3306, 40000)

puts “Connectinge to MySQL Over the tunnel …”

dbh =
‘db_username’, ‘db_password’)

puts “Preparing the query …”
sth = dbh.prepare(“SHOW DATABASES”)

puts “Executing the query …”

while row = sth.fetch_array
puts row


When I run this code, I get below output:

Creating SSH Tunnel …
Connecting to MySQL Over the tunnel …

Meaning, DBI is hanging on connecting over the SSH tunnel. It never
reaches the point where it should have printed “Preparing the query …”

If I create the SSH tunnel on the command line and then try connecting
using the MySQL CLI, then it works just fine. In fact, I am running
mysqldump via ruby by connecting it over the tunnel.

However, in this case, I am using DBI to make the DB connection and it
gets hanged.

Any suggestions?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs