SSH Port Forwarding
In this instance, one could port forward db_server:3306 to web_server:3306. Then it would appear as if there were a MySQL database running locally on the web server listening on port 3306. However, localhost:3306 on the web server is really being securely forwarded to localhost:3306 on the database server.
To set this up, you'll want a password-less key pair to allow the SSH tunnel to be started automagically. Do the following:
db_serv$ ssh-keygen -t rsa
db_serv$ scp .ssh/id_rsa.pub webserver:
web_serv$ cd ~; mkdir .ssh
web_serv$ cat id_rsa.pub >> .ssh/authorized_keys2
web_serv$ chmod -R go-rwx .ssh; rm id_rsa.pub
db_serv$ ssh webserver
The last command should let you SSH from the database server without providing a password. The keypair does the authentication.
The command to open an SSH tunnel is:
db_server$ ssh -f -q -N -R3306:db_server:3306 webserver
You can then test out local database access on the webserver. You'll need to have the permissions set correctly in the MySQL databse for the user and password you're using.
web_serv$mysql -h 127.0.0.1 -P 3306 -u user -p db_name
You'll probably want to add the 'ssh' line above to /etc/rc.d/rc.local (on Red Hat) so that the tunnel gets opened on reboots. Remember if the tunnel goes down, your web app can't access the database.