Localhost Vs. 127.0.0.1 in MySQL_Connect()

localhost vs. 127.0.0.1 in mysql_connect()


  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.

What is the difference between 127.0.0.1 and localhost

Well, the most likely difference is that you still have to do an actual lookup of localhost somewhere.

If you use 127.0.0.1, then (intelligent) software will just turn that directly into an IP address and use it. Some implementations of gethostbyname will detect the dotted format (and presumably the equivalent IPv6 format) and not do a lookup at all.

Otherwise, the name has to be resolved. And there's no guarantee that your hosts file will actually be used for that resolution (first, or at all) so localhost may become a totally different IP address.

By that I mean that, on some systems, a local hosts file can be bypassed. The host.conf file controls this on Linux (and many other Unices).

mysql_connect (localhost / 127.0.0.1) slow on Windows platform

PHP is attempting to open a connection to localhost. Because your computer is connected to your network via IPv6 it's trying the IPv6 version of 'localhost' first, which is which is an IP address of ::1

http://en.wikipedia.org/wiki/IPv6_address#Special_addresses

::1/128 — The loopback address is a unicast localhost address. If an
application in a host sends packets to this address, the IPv6 stack
will loop these packets back on the same virtual interface
(corresponding to 127.0.0.0/8 in IPv4).

It looks like your MySQL server isn't listening to that address, instead it's only bound to an IPv4 address and so once PHP fails to open the connection it falls back and tries to open localhost via IPv4 aka 127.0.0.1

I personally prefer to use either IP addresses or use ether the Windows hosts file or Mac equivalent to define 'fake' domain names and then use those when connecting to MySQL, which resolve to IP addresses. Either way I can know exactly whether an IPv4 or IPv6 address will be used.

Both MySQL and Apache support IPv6 but you have to tell them to use an IPv6 address explicitly. For MySQL see:
http://dev.mysql.com/doc/refman/5.5/en/ipv6-server-config.html

For Apache config see:
http://httpd.apache.org/docs/2.2/bind.html

Apache supports multiple IP addresses so you can use both at once - if the network card in the machine has both an IPv4 and IPv6 address. MySQL only supports one address.

mysql_connect doesn't work with localhost on my Windows machine

Filed a bug with PHP. The problem is with MySQL, and has to do with it not correctly connecting to all possible IP addresses for a domain.

Solution? Don't use host names to connect to databases, if it isn't absolutely necessary. This might also have a positive impact on performance.

Getting a mysql 2002 error only when using localhost (rather than 127.0.0.1) when connecting to mysql with php

The MySQL client magically knows that 'localhost' means the local machine, so it tries to connect to a Unix socket instead of making a network request--which is actually more efficient. The problem here is that you're either not using a Unix socket, or--probably more likely--the MySQL client libraries don't know where to find the Unix socket file.

I wish I could tell you where that configuration lives, but a) I haven't actively used MySQL in over 3 years, and b) I've never used MySQL on OSX.

PHP/mysql requiring 127.0.0.1 on OS X Mavericks

Please read those articles:

  1. http://www.coolestguidesontheplanet.com/downtown/get-apache-mysql-php-and-phpmyadmin-working-osx-109-mavericks

  2. AllowOverride for .htaccess on local machine giving 403 Forbidden

  3. http://coolestguidesontheplanet.com/how-to-install-mcrypt-for-php-on-mac-osx-lion-10-7-development-server/

  4. http://michaelgracie.com/2013/10/29/plugging-mcrypt-into-php-on-mac-os-x-mavericks-10-9/



Related Topics



Leave a reply



Submit