MySQL localhost connect works but 127.0.0.1 does not

The MySQL client library overrides localhost to use a Unix socket instead of a TCP/IP connection to 127.0.0.1. If you can connect to localhost but not 127.0.0.1, that suggests that it's either firewalled or not listening on 127.0.0.1.


The manual at Connecting to the MySQL Server suggests using the following syntax :

shell> mysql --host=127.0.0.1

The explanation is :

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 127.0.0.1, 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. For example:

Tags:

Mysql