MySQL bug prevents you from connecting to custom port on MySQL server

It took me a great deal of time and effort to figure out this. In MySQL-client you can specify hostname and port to connect to a different MySQL instance on a different machine and/or different port rather than default localhost instance on your machine. For example, I have 2 MySQL instances running on two different machine, and one of them is behind firewall. Therefore, I need to use SSH tunnel to forward requests to port 3306 of the machine behind firewall.

Things got little complicated when I tried to connect using --port or -P. Since I used the same password for both MySQL server (which I shouldn’t), it took me a while to figure out I still connect to the localhost instance.

The reason is that, when you specify -P only, mysql will switch to socket mode instead of TCP mode. Here is what you need to do:

mysql -P port --protocol TCP

Adding --protocol TCP will force mysql to use TCP connection, thus will connect to the remote instance instead.

Hope that helps!