You need to establish a port forward between your system and the ssh server.
Add the following to your ~/.ssh/config
file:
Host mysql-tunnel-website.com
LocalForward 3306 localhost:3306
I highly recommend SSH key usage for this. Github's SSH key guide is pretty good. I'm not an R coder, but R might dislike the need to enter a password interactively in a system()
call and SSH keys (when passwordless or when added to an ssh-agent) remove that need.
Now you should be able to start up the tunnel in R with:
system('ssh -f mysql-tunnel-website.com')
This will map website.com's localhost port 3306 to your ssh client's localhost on port 3306, allowing you to run the same code on your ssh client system as you would have on the remote website.com system.
Your R code needs to point to host=localhost
and port=3306
(which should be the default).
If the remote SQL server isn't served by website.com's localhost with port 3306, simply change the localhost:3306
to the appropriate server:port
combination relative to what is accessible from website.com. If you're unable to use port 3306 on your ssh client system (perhaps you're running MySQL locally?), you can forward to a different port by changing that first 3306
to any other port (I tend to prefix a digit like 13306
) and then be sure to specify that alternate port (e.g. port=13306
) in your R code.