30

I would like to create a ruby script that I can run mysql commands on a remote server through a ssh tunnel.

Right now I have a manual process to do this:

  1. Create a tunnel -> ssh -L 3307:127.0.0.1:3306
  2. run ruby script.
  3. Close tunnel.

I would love to be able to automate this so I can just run the script.

example:

require 'rubygems'   
require 'net/ssh/gateway'  
require 'mysql'  


#make the ssh connection -> I don't think I am doing this right.

Net::SSH.start('server','user') do |session|

  session.forward.local(3307,'127.0.0.1', 3306)<br>
  mysql = Mysql.connect("127.0.0.1","root","","",3307)

  dbs = mysql.list_dbs<br>
  dbs.each do |db|<br>
    puts db <br>
  end

  session.loop(0){true}<br>
end

An update - 2010-11-10:
I'm really close with this code:

require 'rubygems'  
require 'mysql'  
require 'net/ssh/gateway'  

gateway = Net::SSH::Gateway.new("host","user",{:verbose => :debug})
port = gateway.open("127.0.0.1",3306,3307)

#  mysql = Mysql.connect("127.0.0.1","user","password","mysql",3307)  
#  puts "here"  
#  mysql.close  

sleep(10)  
gateway.close(port)

When its sleeping, I am able to open a terminal window and connect to mysql on the remote host. This verifies the tunnel is created and working.

The problem now is when I uncomment the 3 lines, it just hangs.

5 Answers 5

29

I was able to get this to work without a fork using the mysql2 gem

require 'rubygems'
require 'mysql2'
require 'net/ssh/gateway'

gateway = Net::SSH::Gateway.new(
  'remotehost.com',
  'username'
 )
port = gateway.open('127.0.0.1', 3306, 3307)

client = Mysql2::Client.new(
  host: "127.0.0.1",
  username: 'dbuser',
  password: 'dbpass',
  database: 'dbname',
  port: port
)
results = client.query("SELECT * FROM projects")
results.each do |row|
  p row
end
client.close
2
  • 5
    just make sure to change port = gateway.open('127.0.0.1', 3306, 3307) to port = gateway.open('myrdsinstance.whatever.com', 3306, 3307) if you are using rds Commented May 14, 2013 at 20:52
  • 1
    Has anyone else gotten stuck after trying to create the Mysql2 client? I'm getting an error: Mysql2::Error: Lost connection to MySQL server at 'reading initial communication packet', system error: 0 Commented Aug 16, 2016 at 15:08
7

This might be one possible solution:

require 'rubygems'  
require 'mysql'  
require 'net/ssh/gateway'  


gateway = Net::SSH::Gateway.new("server","user")  
port = gateway.open("127.0.0.1",3306,3307)

  child = fork do  
    mysql = Mysql.connect("127.0.0.1","user","password","mysql",port)  
    sql = "select sleep(5)"  
    mysql.query(sql)  
    mysql.close  
    exit  
  end  
  puts "child: #{child}"  
Process.wait  
gateway.close(port)  

Maybe there is a better way, but this works for what I was trying to do.

2
  • 2
    It's very strange that it doesn't work without fork, I've tried Redis and Mongo and it worked just fine without any kind of fork. It must be some mysql driver problem.
    – developer
    Commented Feb 15, 2012 at 15:46
  • When I try, I get NameError: uninitialized constant Net::SSH::Service::Forward::UNIXServer. Any idea why?
    – Jellicle
    Commented Jul 16, 2012 at 16:36
4

I've been trying out the gateway code above, one main difference being I have to use ssh keys for passwordless access, but also found the code handing on the Mysql.connect statement. However, when I replaced

mysql = Mysql.connect("127.0.0.1",...)

with

mysql = Mysql.connect("localhost",...)

it worked fine.

My final code looks like this:

require 'rubygems'
require 'mysql'
require 'net/ssh/gateway'

gateway = Net::SSH::Gateway.new('host',
           'user',
           :keys => ['myprivatekey.pem'],
           :verbose => :debug)
port = gateway.open("127.0.0.1",3306,3307)

mysql = Mysql.connect("localhost","dbuser","dbpassword","dbname",3307)
puts "here"
mysql.close

gateway.close(port)
gateway.shutdown!
1
  • Well, this didn't really work - it just connected to mysql on my local machine. Commented Oct 19, 2011 at 23:44
0

Usually, when a tunnel is up binding a local port to the remote application port, you just connect to the local port as if it were the remote one. Remember that MySQL has access policies based on the source location of the connection, so you might want to keep that in mind. In my opinion, there's no session.forward.local nessessary.

Of course, you still don't speak the MySQL connection protocol so this might not be what you want. It might be easier to drop whatever queries to run into a file, then run mysql -u"user" -p"password"

1
  • TME, thanks for the response. I know the access controls are set correctly due to everything working the "manual" way.
    – user498023
    Commented Nov 5, 2010 at 14:57
-1

You can also try this nice ruby gem: https://github.com/progrium/localtunnel

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.