-1

I saw the following statement on StackOverflow and was wondering about its meaning:

If you connect via 'localhost', the connection will automatically be established via the MySQL socket, which is really cheap anyways.

The discussion thread was pretty old, so I didn't want to comment on it.
Basically what I understand is, that using 'localhost' when connecting to your mysql database has certain advantages - such as "automatically established connections via MySQL socket". What does that mean exactly?

Currently I'm using mysql_connect("73.21.24.201", [...]);
(changed to a random IP Address)
Does it make any difference? Can I change it to "localhost" without having to worry about it? (The mysql server is obviously on the same server/ip address as my website/application)

Community
  • 1
  • 1
Tobias Baumeister
  • 2,107
  • 3
  • 21
  • 36
  • Just to clarify, are you using your server's IP address or 127.0.0.1? – JTG Oct 16 '14 at 20:31
  • Do you want to know if that would work or if that would boost performance ? – Alban Pommeret Oct 16 '14 at 20:31
  • JTG: I'm using my server's IP Address. Afaik, 127.0.0.1 is the same as localhost, which wouldn't make a difference, right? – Tobias Baumeister Oct 16 '14 at 20:32
  • 2
    It's just a location. using localhost, the external ip, or the local ip should make little to no difference. The only difference would be whether or not a dns lookup needs to happen, and where that lookup would occur. For example, if you used a domain name that is defined in a dns that isn't local, a dns lookup would have to be made to find the ip of that domain name, and since it wasn't found inside the server itself, it would have to go to an external dns server to find it. localhost does no such lookup. – Kevin B Oct 16 '14 at 20:33
  • Alban: Basically both. I'm hoping for a performance boost though – Tobias Baumeister Oct 16 '14 at 20:33
  • Kevin: So you're saying that using the external IP Address doesn't require a DNS lookup as well, right? Only wenn I use "mywebsite.com" as mysql host? – Tobias Baumeister Oct 16 '14 at 20:36
  • The way i understand it, yes, that wouldn't result in a lookup, it would resolve within your own network. I may be wrong, i'm not a network guru or enthusiast, but i do know that this is a premature optimization and a waste of time to worry about. :) If the mysql instance is on the same server, just use localhost or 127.0.0.1, there's no reason not to. by doing so you won't rely on any dns routing. – Kevin B Oct 16 '14 at 20:38
  • Well I just changed it to "localhost" and the website went down immediatelly... weird. Well anyways. I'm actually just trying to find out why I have approx. 14,000 open mysql sockets (including TIME_WAIT). And I was hoping to find a way to reduce this or use persistent connections or whatever. The quote from the original post made it sound like "localhost" does magic and keeps only a few sockets for every query/connection :D Thanks anways – Tobias Baumeister Oct 16 '14 at 20:44
  • 1
    That would be an application issue and/or a config issue. For example, there's a setting that limits how long a connection can stay open, and it's usually defaulted to some really large number. If your applications don't properly close the connections, you can end up with a ridiculous number of open idle connections. First step to fix that would be to see if you can properly close the connections, and if not, think about updating the config option. – Kevin B Oct 16 '14 at 20:45
  • If this is new code you really shouldn't be using `mysql_connect` in the first place, it's a deprecated interface being removed from PHP. – tadman Oct 16 '14 at 21:02
  • It's not new code - I've been using it for years. I thought about changing it to mysqli, but that would be impossible considering the huge amount of .php files which are using the mysql_query function – Tobias Baumeister Oct 16 '14 at 21:10
  • All this is only true if the MySQL server is running on Unix or Linux, of course, and the statement about mapping `"localhost"` to the Unix socket only appears to be true for the PHP connector. – user207421 Oct 17 '14 at 00:30

1 Answers1

0

When you connect to 'localhost' you'll connect using a Unix socket, which is just a communications channel for the local processes to use. The big advantage of this is that you can disable networking completely in MySQL, and negate any processing overhead and security risks that go along with that.

When MySQL starts, it creates a socket file (typically at a place like /var/lib/mysql/mysql.sock) that your client program needs to be able to find. On a typical PHP (you didn't say, but I'm assuming) setup, it should know where to find this socket. If not, check /etc/my.cnf and /etc/php.ini to make sure the values match.

And finally, if that is PHP, stop using mysql_*() functions in PHP right now! They have been deprecated for years and are inefficient and insecure.

Community
  • 1
  • 1
miken32
  • 42,008
  • 16
  • 111
  • 154