Bug #27361 Using localhost with ssh port forwarding failes to connect -- must use 127.0.0.1
Submitted: 21 Mar 2007 21:59 Modified: 22 Mar 2007 18:18
Reporter: Marc Harris Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL GUI Common Severity:S3 (Non-critical)
Version:5.0r10 (of GUI Tools) OS:Linux (Linux (Suse 10.2))
Assigned to: CPU Architecture:Any

[21 Mar 2007 21:59] Marc Harris
Description:
I tried connect MySQL Administrator to a remote db server using ssh port forwarding. The ssh tunnel seems to be set up correctly (I have other applications set up analogously that work fine).

I believe that if the "Server Hostname" is set to "localhost" then the port is ignored, and instead an alternative mechanism (socket file?) is used. 

How to repeat:
I have a mysql server on my localmachine, and a mysql server on the remote machine, both listening on port 3306.

Scenario 1:
- both servers running
- ssh port 12306 forwarded to port 3306 on remote
- mysql connection hostname: localhost
- mysql connection port: 12306
results: a connection is made to the local db; it should have been to the remote one. (I can tell it is the local db because the dbs have different contents).

Scenario 2:
- shut down the local server
- ssh port 3306 forwarded to port 3306 on remote
- mysql connection hostname: localhost
- mysql connection port: 3306
results: the following error message:
Could not connect to host 'localhost'.
MySQL Error Nr. 2002
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Scenario 3 (exactly the same as scenario 1, except for hostname):
- both servers running
- ssh port 12306 forwarded to port 3306 on remote
- mysql connection hostname: 127.0.0.1
- mysql connection port: 12306
results: a connection is made to the remote db (correctly)

This leads me to believe that when the hostname is localhost, the port number is ignored, and something happens other than an attempt to connect on port 3306.

Suggested fix:
I suggest two parts to the fix:
1) If the port value is supplied, and it is not 3306 (or some other way to determine the port that the local server is listening on), then connect using the correct port, and do not attempt to use the socket file
2) If an attempt is made to connect using the socket file, and the connection fails, then attempt to connect using a regular TCP-IP socket.
[22 Mar 2007 8:50] Sveta Smirnova
Thank you for the report.

But it is because the MySQL Access Privilege System treats localhost value in special way. All clients behaves in such mode. See also http://dev.mysql.com/doc/refman/5.0/en/connection-access.html
[22 Mar 2007 16:03] Marc Harris
I changed the category to "MySQL Network" since I agree it is not a GUI specific issue, and I wasn't sure where else to put it.

I reopened it because, respectfully, I still think that this is the incorrect behavior.

It seems to me that this is an unintended consequence of an optimization. I understand that the specification says that things work this way, but I maintain that the specification is therefore wrong, even though the implementation conforms to it.

"localhost" is not a special MySQL keyword; it's a standard network name. To treat an attempt to connect to localhost:12306 as if it were an attempt to connect to a local server that is listening on port 3306 is not the correct behavior.

No other software I've ever seen behaves this way; the MySQL windows client doesn't behave this way; it is non-standard and very unobvious. While sticking to the standard way of doing things is not always the correct thing to do, there typically needs to be a good reason for it.
[22 Mar 2007 18:18] Sveta Smirnova
Sorry, but this feature has been decided years ago and could not be changed without breaking many applications.

>the MySQL windows client doesn't behave this way;
Read about difference between Windows and UNIX clients behaviour here:
http://dev.mysql.com/doc/refman/5.0/en/invoking-programs.html

I also reverted category of bug report, because "MySQL Network" is service (see also http://www.mysql.com/products/enterprise/whats_new.html) and not category to report about connection issues.