How to connect Mysql server through SSH tunnel using AutoSSH


Read this tutorial first: How to create SSH tunnel on Windows by using AutoSSH

In step 8, we use the command like:

cygrunsrv -I AutoSSH -p /bin/autossh -a "-M 20000 -L 127.0.0.1:1081:68.235.32.101:3306 root@tutorialspots.com" -e AUTOSSH_NTSERVICE=yes

Now, all traffic on local port 1081 will be forwarded to the SSH server on port 3306 (port MYSQL service).

The PHP code for test:

<?php

if(!($c = @mysql_pconnect('localhost:1081','user','pass'))) echo mysql_error();

mysql_select_db('tutorialspots',$c);

$sql = mysql_query("select * from wp_posts where ID = 2954");

var_dump(mysql_fetch_assoc($sql));

?>

Result:

array(23) {
  ["ID"]=>
  string(4) "2954"
  ["post_author"]=>
  string(1) "2"
  ["post_date"]=>
  string(19) "2016-05-21 10:18:42"
  ["post_date_gmt"]=>
  string(19) "2016-05-21 10:18:42"
  ["post_content"]=>
  string(8894) "
......................
"
  ["post_title"]=>
  string(36) "How to install SSH daemon on Windows"
  ["post_excerpt"]=>
  string(0) ""
  ["post_status"]=>
  string(7) "publish"
  ["comment_status"]=>
  string(4) "open"
  ["ping_status"]=>
  string(4) "open"
  ["post_password"]=>
  string(0) ""
  ["post_name"]=>
  string(36) "how-to-install-ssh-daemon-on-windows"
  ["to_ping"]=>
  string(0) ""
  ["pinged"]=>
  string(0) ""
  ["post_modified"]=>
  string(19) "2016-05-21 10:44:02"
  ["post_modified_gmt"]=>
  string(19) "2016-05-21 10:44:02"
  ["post_content_filtered"]=>
  string(0) ""
  ["post_parent"]=>
  string(1) "0"
  ["guid"]=>
  string(32) "http://tutorialspots.com/?p=2954"
  ["menu_order"]=>
  string(1) "0"
  ["post_type"]=>
  string(4) "post"
  ["post_mime_type"]=>
  string(0) ""
  ["comment_count"]=>
  string(1) "0"
}

,

Trackbacks/Pingbacks

  1. How to create SSH tunnel on Windows by using AutoSSH | Free Online Tutorials - May 21, 2016

    […] Example: Read the next tutorial: How to connect Mysql server through SSH tunnel using AutoSSH […]

Leave a Reply