• 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?

CTXMedia

Formerly CyclingTribe
#1
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
 

MattW

Well-known member
#3
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
 

MattW

Well-known member
#7
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.
 

CTXMedia

Formerly CyclingTribe
#8
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.
So for my XF config.php file I want to be using 'localhost' as opposed to 127.0.0.1 ?
 

MattW

Well-known member
#9
So for my XF config.php file I want to be using 'localhost' as opposed to 127.0.0.1 ?
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]#
 

CTXMedia

Formerly CyclingTribe
#10
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?
 

Adam Howard

Well-known member
#12
So for my XF config.php file I want to be using 'localhost' as opposed to 127.0.0.1 ?
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.
 

CTXMedia

Formerly CyclingTribe
#18
Sure. Localhost or 127.0.0.1 on the same box will be sub nanoseconds. It is the same box.
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