View unanswered posts | View active topics It is currently Sun Mar 26, 2017 2:55 am

Reply to topic  [ 4 posts ] 
 connecting to MySQL through ssh2_tunnel 
Author Message
Junior Member

Joined: Thu Jul 24, 2008 4:35 pm
Posts: 1
Post connecting to MySQL through ssh2_tunnel
I'm trying to connect one linux server to another linux server's mysql db using the PHP function ssh_tunnel().

I can access the linux server's mysql db on my Mac by typing in the command line:

ssh username@serverIP -L 3307:

Then I use CocoaMySQL to access the DB.

I want to do about the same thing with PHP to connect one linux server to the other linux server's mysql db. Here's what I've tried:
$connection = ssh2_connect('serverIP', 22);
if (ssh2_auth_password($connection, 'username', 'secret')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');

echo "success";
echo "failure";

$tunnel = ssh2_tunnel($connection, '', 3307);

$conn = mysql_connect('', 'DBusername', 'DBpassword' ) or die (mysql_error());


The ouput I get is:

Authentication Successful!


Access denied for user 'DBusername'@'localhost' (using password: YES)

But If I change the username and password to the local mysql username and password, then I get access to the local mysql db but not the remote mysql db.

Any ideas how to connect to the remote server, through ssh, and use the mysql_connect() function?

Thu Jul 24, 2008 4:55 pm
Report this post
Junior Member

Joined: Tue Nov 10, 2009 2:15 pm
Posts: 2
ssh2_tunnel() returns a socket type resource or something similar.

Then if you want to forward your local ports through the tunnel, you must need to bind a socket to listen in your local client machine, and then write the incoming data through the tunnel using fwrite($tunnel,$data_to_send); or something similar.

I'm now trying to figure out how to send data through the tunnel, fwrite seems doesn't working for this propose, but, this is an approach.

Tue Nov 10, 2009 2:21 pm
Report this post
Junior Member

Joined: Tue Nov 10, 2009 2:15 pm
Posts: 2
This finally produces some results:


//ssh2_tunnel() example.
//forwarding a local client port to server port (only send)

// just run:

// $ netcat -l -k -p <REMOTEPORT>
// on server side, and replace define(REMOTEPORT,XX) with same number port that you've used on netcat's
// command line.

// Then, run this script and open your browser with http://localhost:
// you should see the plain http browser query on your remote SSH2 server terminal

// As in this example, just use:
// $ netcat -l -k -p 8081
// and type http://localhost:8888 on your browser

// Note: this script is only illustrative. Is not something to be used as is in the real world.



function stdout($string) {

function tunnel_disconnect($reason, $message, $language) {
global $setbreak;

printf("Server disconnected with reason code [%d] and message: %s\n",
$reason, $message);

function open_tunnel($host) {
global $setbreak;

$methods = array(
'kex' => 'diffie-hellman-group1-sha1',
'client_to_server' => array(
'crypt' => 'blowfish-cbc,aes256-cbc,3des-cbc',
'comp' => 'none'),
'server_to_client' => array(
'crypt' => 'blowfish-cbc,aes256-cbc,aes192-cbc,aes128-cbc',
'comp' => 'none'));

$callbacks = array('disconnect' => 'tunnel_disconnect');

stdout(" --> Openning SSH2 connection ... ");
$conn = ssh2_connect($host, SSH2SERVER_PORT, $methods);

if (ssh2_auth_password($conn, SSH2USER, SSH2PASS)) {
$tunnel = ssh2_tunnel($conn, '', REMOTEPORT);
if (is_resource($tunnel)) {
stdout(" --> Tunnel sucesfully established.\n");
$sock = socket_create_listen(LOCALPORT);
if(is_resource($sock)) {
stdout(" Local sockets opened sucesfully.\n");
while(!$setbreak) {
if (($localsock = @socket_accept($sock)) !== false) {
stdout(" --> Client Connected\n");
while (is_resource($localsock)) {
$x = @socket_read($localsock, 4096);
if ($x !== false && strlen ($x)) {
stdout(" + Sending data [" . strlen($x) . "] bytes\n");
} else
stdout(" Error openning tunnel!\n");
} else


while (true) {


Tue Nov 10, 2009 3:49 pm
Report this post
Junior Member

Joined: Wed Aug 31, 2011 4:38 am
Posts: 2
Post Re: connecting to MySQL through ssh2_tunnel
Hi, i have the same problem of GREg, it seems there was a change in HostGator MySql settings. I can still remotely connect to MySql servers fine, but NONE OF MY databases appear after I connect, only information_schema... i use my cpanel user access and it is the same.

what it is the trouble?

Thu Sep 01, 2011 12:41 am
Report this post
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
© Copyright 2003-2008 The ultimate PHP Editor and PHP IDE site.