# 127.0.0.1 vs localhost for phpmyadmin and Wordpress

## ferg

Hi,

I've a Gentoo development server that I've not really been keeping on top of! 

I just had a confusing error with Wordpress that couldn't connect to my database. I launched phpmyadmin to test the PHP > MYSQL connection and that could not work either.

Turns out that for both tools I needed to connect to "127.0.0.1" instead of "localhost". So editing the appropriate config files restored functionality.

Has anything like that changed recently? Like in the past six months?

Cheers

Ferg

----------

## ct85711

Typically, 127.0.0.1 is linked to localhost so it would work, however, the link is typically set inside the /etc/hosts file.  You may want to check that file, here's a copy of my hosts file (most times peopkle rarely need to modify the file).

```
# /etc/hosts: Local Host Database

#

# This file describes a number of aliases-to-address mappings for the for 

# local hosts that share this file.

#

# In the presence of the domain name service or NIS, this file may not be 

# consulted at all; see /etc/host.conf for the resolution order.

#

# IPv4 and IPv6 localhost aliases

127.0.0.1   localhost

::1      localhost

#

# Imaginary network.

#10.0.0.2               myname

#10.0.0.3               myfriend

#

# According to RFC 1918, you can use the following IP networks for private 

# nets which will never be connected to the Internet:

#

#       10.0.0.0        -   10.255.255.255

#       172.16.0.0      -   172.31.255.255

#       192.168.0.0     -   192.168.255.255

#

# In case you want to be able to connect directly to the Internet (i.e. not 

# behind a NAT, ADSL router, etc...), you need real official assigned 

# numbers.  Do not try to invent your own network numbers but instead get one 

# from your network provider (if any) or from your regional registry (ARIN, 

# APNIC, LACNIC, RIPE NCC, or AfriNIC.)

#

```

----------

## ferg

Pinging "localhost" still works!

----------

## py-ro

localhost has a special meaning for MySQL (and its drivers), rather connecting via lo, it does connect via a Unix socket, if localhost as server is specified.

----------

## ferg

 *py-ro wrote:*   

> localhost has a special meaning for MySQL (and its drivers), rather connecting via lo, it does connect via a Unix socket, if localhost as server is specified.

 

That makes sense. So my problem was likely that there was a problem connecting via a UNIX socket and I've inadvertently worked around that:

PHP (i.e. PHPmyAdmin and Wordpress) were previously connecting via a UNIX socket. That broke (perhaps the dir change to "/run/mysql/mysqld.sock"?). Changing from "localhost" to "127.0.0.1" forced PHP to connect via TCP/IP instead. Thus working around the socket problem.

So I should fix the socket problem I guess.

----------

## UberLord

 *ferg wrote:*   

> So I should fix the socket problem I guess.

 

Yes, no maybe  :Smile: 

Sockets are good if speed really really matters.

loopback interfaces have a stupid large MTU so there should be no/little fragmentation so the only overhead you have is that the packet still needs to traverse the whole network stack whereas a socket doesn't.

Sockets have acl by default (it's a file, so same semantics) whereas loopback has none.

On the other hand ......

loopback interfaces are generally better tested and it's easier to say another host instead of localhost if you need to spread the load across boxes.

I personally fine loopbacks easier to manage incase the local idiot (me on a bad day) hoses the socket file.

Also, the startup order of loopbacks is a lot more flexible (see socket activation issues in systemd and friends).

----------

