php mysql_connect(): How to connect to MySQL server through SSH tunnel
My mysql server is being hosted somewhere outside and requires to ssh in order to reach there, how do I connect this server through php ? Anyone has experienced same problem before?
Thanks in advanced.
I haven't but googling I found the following example:
<?php shell_exec("ssh -f -L 3307:127.0.0.1:3306 firstname.lastname@example.org sleep 60 >> logfile"); $db = mysql_connect('127.0.0.1', 'sqluser', 'sqlpassword', 'rjmadmin', 3307); ?>
- It seems you first need to setup a ssh tunel (timeout of 60s). This works at OS level (shell_exec);
- In this example, your creating a SSH tunnel redirecting the 3306 port on the mysql host server to the port 3307 locally (webserver);
- You then connect (mysql_connect) as the mysql server were in your own webserver, redirect to the 3307 port.
I could't validate this solution, but I hope this helps you a step forward.
Not sure if either of these will apply to you, but a quick search found some useful results: