localhost vs. 127.0.0.1 in mysql_connect()
Asked Answered
B

7

60
  1. Does using localhost in mysql_connect() make the connection faster than using 127.0.0.1?
  2. What is the connection type between the PHP script and mySQL (when using the mysql_connect() function) ? Is it TCP/IP?
Belia answered 15/9, 2010 at 8:33 Comment(0)
M
84
  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.
Marcel answered 15/9, 2010 at 8:36 Comment(8)
Thank you! Why doesn't Apache interact through TCP/IP or socket based connection with PHP?Belia
Apache uses either mod_php or connects to PHP using TCP/IP or Unix Domain Socket (when configured to run via fastcgi).Marcel
+ the nasty thing in linux is when you specify 'localhost' as host, and a specific port, it just ignores the whole port bit and uses the default socket, not something one wants when running multiple servers on a single machine (hence the different port..).Rosina
@Wrikken, that's true, but easily avoidable by using 127.0.0.1:portMarcel
@halfdan: indeed, but something one needs to know before proceeding to demolish some tables :P Ah, live and learn, it was a while back and I'll never forget it.Rosina
also, 127.1 is synonymous with 127.0.0.1 . try it.Ulrikaumeko
@djangofan: No? 127.1 is not a valid IP address. It could work as a fallback by the database driver.Marcel
In Windows 10 and 11 is significantly faster to use 127.0.0.1!Epigastrium
E
21

"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.

Expert answered 15/9, 2010 at 8:37 Comment(3)
TCP/IP is also socket based. I know what you mean, but you'll confuse people with it.Marcel
It could be Unix-socket connectionPremarital
You are right. I changed this to "local socket" to clarify. Thanks for noticing.Expert
O
17

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.

Overmaster answered 26/9, 2012 at 11:27 Comment(2)
Had the same experience. Going from localhost to 127.0.0.1 dropped my latency from 1 second to 5 ms. Thanks!Madisonmadlen
That's because windows had dropped localhost from the hosts file in later versions so it takes a long time to resolve.Lilith
C
2

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

protocol=tcp
Choline answered 19/12, 2012 at 18:25 Comment(0)
L
0

Php site says:

Note:

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.

I guess the speed difference would be too low that it's something you should'nt be worried about.

Lotic answered 15/9, 2010 at 8:41 Comment(0)
J
0

For macs, here's the solution:

Connect to MySQL using localhost instead of 127.0.0.1 on a MAC. For a long while now I’ve been connecting to MySQL on my development platform with 127.0.0.1 because for some reason localhost didn’t work. Turns out it’s because 127.0.0.1 uses TCP/IP and localhost uses sockets. The php.ini file points to the wrong place for the mysql.sock so all you have to do is change it, restart apache and voila!

Open php.ini: /private/etc/php.ini
Find the following line: mysql.default_socket = /var/mysql/mysql.sock
Replace with: mysql.default_socket = /tmp/mysql.sock
Restart apache: apachectl restart

Note: If you don’t have a php.ini file, you need to copy the provided default called php.ini.default

sudo cp /private/etc/php.ini.default /private/etc/php.ini

via http://madproject.com/general/connect-to-mysql-using-localhost-instead-of-127-0-0-1-on-a-mac/

Jimmie answered 24/10, 2013 at 0:7 Comment(1)
Slight clarification. When you wrote "it’s because 127.0.0.1 uses TCP/IP and localhost uses sockets", a careless reader might believe that, somehow, Macs have a different understanding of how sockets work. They don't. What you mean to say was "it's because MySQL uses Internet Domain Sockets to connect to 127.0.0.1, but uses Unix Domain Sockets when connecting to localhost (for performance issues)". Also, this is unrelated to the issue you actually describe — which is how the default php.ini might have incorrect data for MySQL.Wentworth
H
0

It is not true that localhost is faster, but is rather a bit slower.

That being said, if you are connecting to someone else; catch my drift?

Who is to say which side of the connection fails harder? No one. That:

127.0.0.1 is a faster ping than localhost. Try on any Terminal.

Hedjaz answered 23/7, 2021 at 23:13 Comment(2)
Right @Php site! The difference is insubstantial to most users... LET US NOT LEAVE IT THERE!Hedjaz
"127.0.0.1 is a faster ping than localhost". If you mean that the round-trip time for a packet to be measured using the ping system tool arrives sooner from 127.0.0.1 than from localhost, then of course you're utterly and absolutely wrong :) The worst that may happen is that DNS needs to resolve localhost first, thus the first packet might be slower, but the rest is one and the same interface, and traffic never leaves your system anyway. So, no :) Unless you mean something entirely different on a very specific scenario on one particular OS. In that case, please clarify!Wentworth

© 2022 - 2024 — McMap. All rights reserved.