Rodrigo Rosenfeld Rosas
MySql localhost behavior is totally insane
I always have this issue so I found it would be better to document it for myself.
I never really liked MySql, preferring PostgreSQL instead for multiple reasons, but that is not what this article is about. While I don’t migrate the current database to PostgreSQL (everything is already set up, just waiting for permission for doing so), I’ll probably have this issue many more times.
I have some port redirects to my application production database servers. And I always try to access using this command line (or through some library API, it doesn’t make any difference):
1 | mysql -h localhost -P 3307 -u my_user -p my_database_name |
The problem is that I succeed on doing that, but I’m actually using my local database using sockets. WTF?!
Since I have the same users and passwords in my local database, it doesn’t complain, but it completely ignores the -P (–port) argument and I think I’m accessing the right database. There are two fixes for that. The most simple one and that will also work in my software configurations is to use 127.0.0.1 instead of localhost.
For the command line, you can use also add the –protocol=tcp argument:
1 | mysql --protocol=tcp -h localhost -P 3307 -u my_user -p my_database_name |
Now I don’t understand why the protocol isn’t set to TCP automatically when you specify a TCP port in the program arguments! This is just dumb! And worse than that is to find out that the state for this bug is “Not a bug”. I really hate MySql.