Note: this is similar to Use Ruby on Rails and SSH to access remote MySQL database on remote server, but the OP didn\'t provide much info, and the only answer given does
There is also a pure rails solution
add the following to your Gemfile
gem 'net-ssh-gateway'
then create a class
module RemoteConnectionManager
SSH_USER = 'YOUR_SSH_USER'
def self.port_through_tunnel(remote_host, port, local_port: nil, db_host:'localhost')
return Net::SSH::Gateway.new(remote_host, SSH_USER)
.open(db_host,port,local_port)
end
end
last change your database.yml
adapter: mysql2
host: 127.0.0.1
port: <%= RemoteConnectionManager.port_through_tunnel('your_ssh_host', 3306, db_host: 'your_db_host_eg_some_aws_rds_db' ) %>
username: your_db_username
password: your_db_password
database: your_db_name
if local_port is nil Net/ssh will pick a free one