1

I was debugging some code and realised that a method was taking almost 2 seconds to connect to MySQL. After a bit of digging (Why is constructing PDO connection slow?) I found that it's because I was using 'localhost' instead of '127.0.0.1'. This sped it up significantly. My question is, why? Why does a lookup in my /etc/hosts take so long? I would've thought it was really quick. Also, this optimisation doesn't speed anything up on my work desktop, but definitely worked on my laptop (they're both running Ubuntu 12.04)

Edit: My question is why does it take so long. I understand that the comment in the linked question says that lookups take a long time, but I am asking why.

Community
  • 1
  • 1
Matthew Haworth
  • 426
  • 1
  • 5
  • 17

2 Answers2

0

It might have something to do with sockets and the way MySQL treats localhost. See here.

mrjink
  • 1,131
  • 1
  • 17
  • 28
0

Could it be more about the inverse resolution? Normally it is really a better idea to user "real" names and IPs, even if it is a local connection. Try using the hostname of the server and the IP.

jordi
  • 1,157
  • 1
  • 13
  • 37
  • What do you mean by inverse resolution? – Matthew Haworth Jan 23 '14 at 22:28
  • When you translate a name to an IP address is direct resolution and when IP is translated to the name that is the reverse resolution (or inverse, I have read it in both ways). Some client-server comunications have delay issues when trying to check who is on the other side, by asking for the reverse resolution of the hostname that originated the conection. 127.0.0.1 is a special case and maybe it is not being translated to locahost. Thats one reason why I normally prefer real IPs and names. – jordi Jan 24 '14 at 07:22