0
這是從5年前到「How to create a ssh tunnel in ruby and then connect to mysql server on the remote host」的後續問題。將數據從遠程MySQL服務器遷移到Rails應用程序
我想在Ruby中創建SSH隧道,然後連接到遠程MySQL數據庫。我這樣做是因爲我需要從我的應用程序的舊版本移動一些遺留數據,所以我不需要ActiveRecord,遷移等。
我正在使用NetSSHGateway gem和MySQL2 gem。
這裏是我的Ruby代碼(在軌控制檯上運行):
gateway = Net::SSH::Gateway.new('old_remote_server.com','server_username')
port = gateway.open('127.0.0.1', 3306, 3307)
client = Mysql2::Client.new(
host: "127.0.0.1",
username: 'database_username',
password: 'database_password',
database: 'database_name',
port: port
)
最後一行後,控制檯掛起大約2分鐘,然後給我的錯誤:
Mysql2::Error: Lost connection to MySQL server at 'reading initial communication packet', system error: 0
我能夠通過SSH連接到遠程服務器,並以這種方式執行MySQL命令,所以我不確定這裏存在什麼問題。
我建議看看Sequel ORM。除了是一個有用的ORM外,它還能夠輕鬆地將數據從一個DBM遷移到另一個DBM。請參閱http://sequel.jeremyevans.net/rdoc/files/doc/bin_sequel_rdoc.html#label-Copy+Databases –