我想创建一个ruby脚本,我可以通过ssh隧道在远程服务器上运行mysql命令.
现在我有一个手动过程来执行此操作:
创建一个隧道 - > ssh -L 3307:127.0.0.1:3306
运行ruby脚本.
关闭隧道.
我希望能够自动化这个,所以我可以运行脚本.
例:
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)
mysql = Mysql.connect("127.0.0.1","root","","",3307) dbs = mysql.list_dbs
dbs.each do |db|
puts db
end session.loop(0){true}
end
更新 - 2010-11-10:
我真的很接近这个代码:
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)
当它休眠时,我能够打开终端窗口并连接到远程主机上的mysql.这将验证隧道是否已创建并正常工作.
现在的问题是,当我取消注释3行时,它只是挂起.
我能够使用mysql2 gem在没有fork的情况下使用它
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
这可能是一种可能的解决方案:
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)
也许有更好的方法,但这适用于我试图做的事情.