Reputation: 1659
localhost
in mysql_connect()
make the connection faster than using 127.0.0.1
?mysql_connect()
function) ? Is it TCP/IP?Upvotes: 62
Views: 56133
Reputation: 1
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.
Upvotes: -1
Reputation: 4379
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/
Upvotes: -1
Reputation: 34234
Upvotes: 85
Reputation: 27
For Unix, add to the client block after
[client]
in /etc/mysql/my.cnf this line :
protocol=tcp
Upvotes: 1
Reputation: 187
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.
Upvotes: 17
Reputation: 2406
"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.
Upvotes: 21
Reputation: 5980
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.
Upvotes: 0