How to create a ssh tunnel in ruby and then connect to mysql server on the remote host

前端 未结 5 1389
有刺的猬
有刺的猬 2020-12-12 20:56

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:

相关标签:
5条回答
  • 2020-12-12 21:35

    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"

    0 讨论(0)
  • 2020-12-12 21:39

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

    0 讨论(0)
  • 2020-12-12 21:43

    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 讨论(0)
  • 2020-12-12 21:46

    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!
    
    0 讨论(0)
  • 2020-12-12 21:47

    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.

    0 讨论(0)
提交回复
热议问题