I was trying to connect to a remote database using an SSH tunnel as described at http://mywiki.wooledge.org/SshTunnel. My SSH tunnel is using port number 3307 on localhost using a command like this:
$ ssh -L 3307:mysql.example.com:3306 -N -f user@www.example.com
At first, my db settings looked like this:
$databases['default']['default'] = array(
'driver' => 'mysql',
'database' => 'db_name',
'username' => 'user',
'password' => 'pass',
'host' => 'localhost',
'port' => 3307,
'prefix' => '',
'collation' => 'utf8_general_ci',
);
I got a "Can't connect to local MySQL server through socket" error and I noticed that Drupal was in fact trying to connect to the db running on my localhost on port 3306. I could tell that the SSH tunnel was working fine, because my db management tool was able to reach the db server through the tunnel.
I was able to solve this by setting the 'host' value to '127.0.0.1' instead of 'localhost'. It seems that the db layer ignores the port number when the host name is 'localhost'. Is that by design, or is it a bug? Is this in some way related to #26836: Socket Connection/path support (especially on PostgreSQL)?
Comments
Comment #1
damien tournoud commentedThat's how PDO works. Nothing we can do about it.