1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

127.0.0.1 or localhost?

Discussion in 'Server Configuration and Hosting' started by CyclingTribe, Mar 28, 2013.

  1. CyclingTribe

    CyclingTribe Well-Known Member

    When a machine refers to itself in config files etc. is it better to use the IP address (127.0.0.1) or DNS name (localhost)?

    ISTR reading somewhere that the IP address was the most efficient because it removed the need for a name lookup/resolution, but I'm not convinved that local host does a lookup?

    Anyone know - or are they the same?

    Cheers,
    Shaun :D
     
  2. Slavik

    Slavik XenForo Moderator Staff Member

    127.0.0.1 will be faster, though the localhost lookup will be a few ms at most.
     
    Adam Howard likes this.
  3. MattW

    MattW Well-Known Member

    127.0.0.1 won't do a lookup in your /etc/hosts file, but that will probably be really small anyway, so you'll not really notice any difference.

    Code:
    [root@vps2 elasticsearch]#cat /etc/hosts
    127.0.0.1              localhost.localdomain localhost
    xxx.xxx.xxx.xxx                    vps2.z22se.com
     
  4. MattW

    MattW Well-Known Member

    Also, won't localhost try to connect to a unix socket in the first instance?
     
  5. Mike

    Mike XenForo Developer Staff Member

    Yeah, I was going to comment on that.
     
    euantor and MattW like this.
  6. CyclingTribe

    CyclingTribe Well-Known Member

    Is that a good or a bad thing? And would it be required in some instances?
     
  7. MattW

    MattW Well-Known Member

    unix socket supposed to be faster than tcp/ip, because it doesn't have the tcp/ip overhead. It's also secure for stuff like MySQL where you can only connect to it on the local machine.
     
    CyclingTribe and Mike like this.
  8. CyclingTribe

    CyclingTribe Well-Known Member

    So for my XF config.php file I want to be using 'localhost' as opposed to 127.0.0.1 ?
     
  9. MattW

    MattW Well-Known Member

    Yeah, then it will connect to the socket for MySQL, as opposed to tcp port 3306.

    Do a netstat -an | grep 3306 is see if you currently have any connections to MySQL on that port.

    I have skip-networking enabled in MySQL, so it doesn't even port a listen on that port

    Code:
    root@vps [/home/z22se/public_html/forum/library]# netstat -an | grep 3306
    root@vps [/home/z22se/public_html/forum/library]# 
     
    Hoffi likes this.
  10. CyclingTribe

    CyclingTribe Well-Known Member

    Code:
    root:/# netstat -an | grep 3306
    tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
    
    Presumably it would be better to use localhost for my backend cache server name instead of 127.0.0.1 too?
     
  11. MattW

    MattW Well-Known Member

    What do you use for the back end cache?
     
  12. Adam Howard

    Adam Howard Well-Known Member

    I'm going to argue that it varies depending on setup. I had one host where 127.0.01 was fast and others where localhost was faster.

    Of course these were in shared hosting setups, so I'm sure there were other factors.
     
  13. CyclingTribe

    CyclingTribe Well-Known Member

    Libmemcached backend on Debian dedicated server with Percona MySQL.
     
  14. Deebs

    Deebs Well-Known Member

    These types of threads urge me to kill things.
     
    euantor and Luke F like this.
  15. MattW

    MattW Well-Known Member

    Care to expand?
     
  16. Deebs

    Deebs Well-Known Member

    Sure. Localhost or 127.0.0.1 on the same box will be sub nanoseconds. It is the same box.
     
  17. MattW

    MattW Well-Known Member

    True, but that's not the only thing that has been discussed in this thread.
     
  18. CyclingTribe

    CyclingTribe Well-Known Member

    Well I personally don't need to save time - I have a 64GB RAM SSD server that coasts even at busy times - but I was interested in the difference and whether using one or the other was faster / a better convention / or made a practical or programatic difference?

    If there's a good practical reason for using localhost then that's what I'd like to do; but if there's absolutely no difference that's good to know too. (y)

    Cheers,
    Shaun :D
     
  19. MattW

    MattW Well-Known Member

    CyclingTribe likes this.
  20. Deebs

    Deebs Well-Known Member

    Sysadmins will use localhost. It will resolve to their local box where the command was run.
     
    CyclingTribe likes this.

Share This Page