localhost vs. 127.0.0.1 in mysql_connect()

Solution 1:

  1. Differs between Windows and Linux. If you use a unix domain socket it'll be slightly faster than using TCP/IP (because of the less overhead you have).
  2. Windows is using TCP/IP as a default, whereas Linux tries to use a Unix Domain Socket if you choose localhost and TCP/IP if you take 127.0.0.1.

Solution 2:

"localhost" means local socket connection while 127.0.0.1 is TCP/IP. And yes, sockets are faster than TCP/IP.

Cite from http://pl.php.net/mysql_connect

Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.

Solution 3:

Nope, the 127.0.0.1 is recommended to use , because Windows 7 has a problem with choosing between IPv6 & IPv4. I tried this and if I used localhost, the page has reloading about 1sec (1,04sec) and when i used 127.0.0.1, the page has reloading 50ms. Both were used under Windows 7.
In Windows XP it doesn´t make difference.

Solution 4:

For Unix, add to the client block after [client] in /etc/mysql/my.cnf this line :

protocol=tcp