Bug #58811 mysql client connects via socket even when --port and --host is specified
Submitted: 8 Dec 2010 10:57 Modified: 8 Dec 2010 12:14
Reporter: Steven Hartland Email Updates:
Status: Not a Bug Impact on me:
Category:MySQL Server: Command-line Clients Severity:S1 (Critical)
Version:5.0.90 OS:FreeBSD
Assigned to: CPU Architecture:Any
Tags: MySQL, socket, tcp

[8 Dec 2010 10:57] Steven Hartland
When running mysql client to connect to a database on the local machine using tcp by specifying --port and optionally --host the client will still use socket connection under unix.

This could potentially be "critical" issue if the host runs multiple instances and the user connects to the wrong instance, as they won't know this has happened.

How to repeat:
Try to connect to a DB on the localhost running on a none standard port, while a second db is running on the standard port e.g.
mysql --user=root --port=3307 --host=localhost --password=mypassword

This silently ignores both --port and --host and instead tries to connect via the default unix socket e.g. /tmp/mysql.sock

Suggested fix:
If --port or --host is specified then the client should force the use of tcp to ensure that the correct DB instance is used.

A workaround is currently to manually force tcp using --protocol=tcp
[8 Dec 2010 11:35] Peter Laursen
I think it would also do the trick to specify


(but I have not much clue about FreeBSD)
[8 Dec 2010 11:36] Shane Bester
You probably should use -h127.0.0.1 since -hlocalhost is designed to use a socket.
[8 Dec 2010 11:44] Steven Hartland
Any --host=<name> other than "localhost" does indeed trigger tcp but you shouldn't even need --host if your specifying --port and want localhost.
[8 Dec 2010 11:48] Valeriy Kravchuk
Please, read the manual, http://dev.mysql.com/doc/refman/5.1/en/connecting.html:

"On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option."
[8 Dec 2010 12:14] Steven Hartland
Sorry that's not really good enough. This could easily cause serious data loss, and already has here, lost hours of transaction because a table import connected and overwrote data in the wrong instance!

I should not have read a web page to ensure I don't destroy my data, besides which is not mentioned in either command line help or the man page, which are the key sources for information when users are actually using.

The key issue it is VERY dangerous to silently ignore options which the user has specifically requested, when this could lead to connecting to the wrong instance.

I see no reason why --port should be ignored in any instance and hence given the above I would suggest the correct and safe behaviour would be to always use tcp protocol if a --port option is specified.