Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

I get this error:

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

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

ssh -f -L 3307:localhost:3306 sleep 10 >> /var/log/apache2/sshlog

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

share|improve this question
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! – John Zumbrum Feb 8 '12 at 14:35

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 but using SSH to Your $dbhost line should probably have instead of

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 I'll give it a try though this evening. Thanks! – John Zumbrum Feb 8 '12 at 14:35
If your remote MySQL instance is only listening on its loopback interface ( ), 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

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

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! – John Zumbrum Feb 8 '12 at 14:36

Your Answer


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.