如何使用 Ruby Net::SSH或Net::SSH::Gateway gems 和密钥(而不是密码)身份验证远程(从基于云的应用程序)连接到基于 VPS 的 MySQL 数据库?
然后使用 Sequel 或 DataMapper 连接到数据库。我假设在我设法使 SSH 连接正常工作后,我只需设置一个到 'sql_user@localhost:3306/database' 的 Sequel/DM 连接。
我确实在这里找到了几个类似的问题,但它们都使用密码身份验证,而不是密钥,并且只演示了执行原始命令来查询数据库。
更新:我似乎无法让这个(Net::SSH with key manager)工作。
UPDATE2:好的,当我从具有存储在用户本地 .ssh 文件夹中的授权密钥的计算机登录时,我已经设法获得授权,具有以下内容(端口是我在 VPS 上的自定义 SQL 端口):
sql_gate = Net::SSH::Gateway.new('192.xxx.xxx.xx','sqluser', port: 26000)
但是,我将无法.ssh
在应用程序的 VM 中创建文件夹,因此我需要以某种方式传递路径和文件名(我将为指定用户的 SQL 访问创建一个公钥)作为一个选项......但是没有无法弄清楚如何。
更新:现在只需要弄清楚 DataMapper 的访问权限。当前正在测试的代码(remote_user_sql
是我的 Ubuntu 用户,是具有特权sql_user
的 MySQL 数据库用户):localhost/127.0.0.1
require 'net/ssh/gateway'
require 'data_mapper'
require 'dm-mysql-adapter'
class User
include DataMapp......
.
.
end
ssh_gate = Net::SSH::Gateway.new('192.n.n.n','remote_user_sql', {port: 25000, keys: ["sql_rsa"], keys_only: true})
port = ssh_gate.open('localhost',3306,3307)
child = fork do
DataMapper.setup(:default, {
adapter: 'mysql',
database: 'sql_test',
username: 'sql_user',
password: 'passwd',
host: 'localhost',
port: port})
DataMapper.auto_upgrade!
exit
end
puts "child: #{child}"
Process.wait
ssh_gate.close(port)