Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

PHP Code
I've got a development environment, and am trying to access a remote database. Here's the code I'm using:

$dbhost = '127.0.0.1:3307';
$dbuser = 'mydbuser';
$dbpassword = 'mydbpassword';
$dbdatabase = 'mydatabase';
$db=mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);

I get this error:

[error] [client 127.0.0.1] PHP Warning:  mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)


Shell
To make that work, I'm trying to SSH from my regular user account (jzumbrum) like this:

ssh -f -L 3307:localhost:3306 root@remoteserver.com sleep 10 >> /var/log/apache2/sshlog

Question
What commands exist to verify that the ssh port binding worked correctly?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Your PHP message suggests that you're trying to connect to MySQL over the unix socket, not on the network on the localhost interface. Check your configuration again.

You can check your SSH port forwarding by opening up another terminal and doing telnet localhost 3307. You should get some sort of response if the tunnel is up.

share|improve this answer
    
Sweet, will give it a try, thanks for the quick response! –  JohnZ Feb 8 '12 at 14:35
add comment

That error message indicates that PHP is still trying to connect via the socket and not via the network. Your SSH solution will not work until you solve this issue because it relies on connecting via the network.

There are a number of reasons why PHP might be doing this including "SQL safe mode" and the PHP MySQL library overriding certain default values such as 'localhost'. Some of this is documented in the mysql_connect() manual page.

The biggest problem I see in the code is that you are trying to connect to MySQL on 127.0.0.1 but using SSH to remoteserver.com. Your $dbhost line should probably have remoteserver.com instead of 127.0.0.1.

share|improve this answer
    
Hmm, I was under the impression that it's not the remote ip you use, but rather the ip address that mysql is bound to on the remote server, which would be 127.0.0.1. I'll give it a try though this evening. Thanks! –  JohnZ Feb 8 '12 at 14:35
    
If your remote MySQL instance is only listening on its loopback interface ( 127.0.0.1 ), then your SSH tunnel solution is the best idea. You will still need to make PHP connect via the network and not via the socket. –  Ladadadada Feb 8 '12 at 14:41
add comment

I usually do something like this to verify that port forwarding works:

On remote machine (with remote db) do trivial server:

remote$ echo "hello" | nc -l -p 2222

On your local machine initiate port forwarding:

local$ ssh -L 1234:localhost:2222 root@remoteserver.com

and try to connect to simple server:

local$ nc localhost 1234

If you will see "hello" that means port forwarding works as expected.

share|improve this answer
    
Awesome. Thanks for the simple test! –  JohnZ Feb 8 '12 at 14:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.