Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MYSQL - Difference between 127.0.0.1 and localhost

Tags:

php

mysql

I am encountering a strange problem on my imac running OS X. For my frameworks and CMS configuration files if I use host: localhost (in db settings) an error is produced and the CMS/framework cannot connect to the database. However if I replace localhost to 127.0.0.1 everything seems to work fine. Since localhost is resolved to 127.0.0.1 I'd like to know what might be the problem with my php/mysql configurations that is resulting into this behavior.? I encountered this problem with Wordpress, Cake and CodeIgniter.

like image 328
Sarthak Gupta Avatar asked Mar 15 '12 06:03

Sarthak Gupta


People also ask

Is localhost and 127.0 0.1 the same?

Localhost is often considered synonymous with the IP address 127.0. 0.1.

Is it better to use 127.0 0.1 or localhost?

localhost can resolve to an IPv6 address, whereas 127.0. 0.1 cannot. Using localhost on an IPv6 only system can work, whereas 127.0. 0.1 will not since IPv4 is not available.

Is loopback address same as localhost?

The loopback address, also called localhost, is probably familiar to you. It is an internal address that routes back to the local system. The loopback address in IPv4 is 127.0. 01.

Is localhost and IP address the same?

Localhost is a hostname that refers to the local machine currently making the request. On many computers, localhost is an alias for the IP address 127.0. 0.1. When a computer pings this IP address, it is communicating with itself.


1 Answers

localhost will cause the MySQL client to try a UNIX socket in a standard directory. If that doesn't exist or is somewhere else, you won't be able to connect. 127.0.0.1 always uses a TCP connection.

See the manual, C.5.2.2. Can't connect to [local] MySQL server.

like image 120
deceze Avatar answered Oct 25 '22 17:10

deceze