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.

Answers


I haven't but googling I found the following example:

<?php
  shell_exec("ssh -f -L 3307:127.0.0.1:3306 user@remote.rjmetrics.com 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.

(Source)


Not sure if either of these will apply to you, but a quick search found some useful results:

http://www.howtogeek.com/howto/ubuntu/access-your-mysql-server-remotely-over-ssh/

http://quintagroup.com/services/support/tutorials/mysql-linux

http://quintagroup.com/services/support/tutorials/mysql-windows

Reference: http://www.google.com/search?q=connect+to+mysql+server+through+ssh+tunnel&sourceid=ie7&rls=com.microsoft:en-us:IE-SearchBox&ie=&oe=


Need Your Help

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

c# mongodb linq mongodb-.net-driver

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