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:

  shell_exec("ssh -f -L 3307: sleep 60 >> logfile");         
  $db = mysql_connect('', '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:


Need Your Help

Unable to determine the serialization information when trying to query the child MongoDB C#

c# mongodb linq

Um sharing my code Im using to query and get a matching child element to do an update to an element in an array in mongoDB Collection