Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7718753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:14:39+00:00 2026-06-01T03:14:39+00:00

I would like to create a ruby script that I can run mysql commands

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:14:41+00:00Added an answer on June 1, 2026 at 3:14 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want create a ruby script that I can run on the command line
I would like to run for example a synchronizing ruby script (in rails action
I would like to create a COM object that runs in an out of
I would like to create a class whose methods can be called from multiple
I would like to create a stored procedure with parameters that indicate which fields
I was trying to create a quick little script that would insert data into
I would like to open OSX windows (and Windows windows) from a ruby script.
I've only just started learning ruby on rails and I would like to create
I would like to create something like this using ruby on rails. Im not
i would like create a array of structure which have a dynamic array :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.