这是 5 年前“How to create a ssh tunnel in ruby and then connect to mysql server on the remote host”的后续问题。
我正在尝试在 Ruby 中创建 SSH 隧道,然后连接到远程 MySQL 数据库。我这样做是因为我需要从旧版本的应用程序中移动一些旧数据,因此我不需要 ActiveRecord、迁移等。
我正在使用NetSSHGateway gem和 MySQL2 gem .
这是我的 Ruby 代码(在 Rails 控制台中运行):
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 命令,所以我不确定这里的问题是什么。
最佳答案
经过一番思考,出于某些原因,这可能不是迁移数据的最佳方式。
最后,我只是转储了旧版 MySQL 数据库,将其移至新应用程序的环境中,并使用 MySQL2 gem构建 rake 任务以将数据转换并移动到我的新应用程序中。
关于mysql - 将数据从远程 MySQL 服务器迁移到 Rails App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977955/