tags:

views:

182

answers:

3

Hey,

  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?

Thanks

Joel

+11  A: 

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.

halfdan
Thank you! Why doesn't Apache interact through TCP/IP or socket based connection with PHP?
Joel
Apache uses either mod_php or connects to PHP using TCP/IP or Unix Domain Socket (when configured to run via fastcgi).
halfdan
+ 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..).
Wrikken
@Wrikken, that's true, but easily avoidable by using 127.0.0.1:port
halfdan
@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.
Wrikken
also, 127.1 is synonymous with 127.0.0.1 . try it.
djangofan
@djangofan: No? 127.1 is not a valid IP address. It _could_ work as a fallback by the database driver.
halfdan
+2  A: 

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

Piotr Pankowski
TCP/IP is also socket based. I know what you mean, but you'll confuse people with it.
halfdan
It could be `Unix-socket connection`
helios
You are right. I changed this to "local socket" to clarify. Thanks for noticing.
Piotr Pankowski
+1  A: 

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.

Sinan