117

This is what I'm doing:

mysql --host=localhost --port=9999 mysql -u root -p --execute="show tables;"

The command works (connecting to port 3306) no matter what I provide in --port argument. I have two mysql servers running on one machine, and want to connect to the second one by explicitly providing its port number. What's going on? Why does mysql ignore this parameter?

chriscz
  • 5
  • 2
yegor256
  • 1,806
  • 3
  • 16
  • 29

1 Answers1

205

When localhost parameter given, MySQL uses sockets. Use 127.0.0.1 instead.

yegor256
  • 1,806
  • 3
  • 16
  • 29
HUB
  • 6,322
  • 3
  • 22
  • 22
  • 22
    Quite infuriating that this fact isn't mentioned on the mysql man page. – Janek Nov 15 '16 at 15:56
  • 2
    Besides accepted answer here, a bit more explanation can be found in this [mysql bugreport](https://bugs.mysql.com/bug.php?id=55796) and also link to [man page](https://dev.mysql.com/doc/refman/5.7/en/connecting.html). As one of commenters in bugreport page, I also don't understand why doesn't client at least throw a warning in case of using 'localhost' in connection string. – bukva-ziu Mar 26 '17 at 08:51
  • 3
    You're a life saver man! – Touqeer Shafi Oct 17 '17 at 11:56
  • i'm using mysql for a decade now, but this did not crossed my way, yet ... – pscheit Dec 26 '17 at 20:25
  • 4
    Wow, unbelievable that this insane bug is still present 10 years later.. I guess that says a lot about Oracle as well as MariaDb teams. To be clear: the client actually takes the port command, it complains if the port is wrong but it ignores it and connects by socket instead. I started debugging the binary when I found this answer. – John Dec 20 '19 at 04:23
  • 1
    A bug left long enough becomes expected behaviour which cannot be changed otherwise you'd break backwards compatability. – Jonno_FTW Apr 02 '20 at 07:21
  • @Jonno_FTW They have changed a ton of features in mysql over the past years, breaking backwards compatibility in so many horrible ways. They even shut off their entire caching features, that alone took me hundreds of hours to compensate. It's not a compatibility problem, it's just lack of professionalism. It plagues many opensource projects. They have devs who are often not very reasonable – John Dec 26 '21 at 01:30