Tuesday, December 11, 2012

MySQL: Localhost vs 127.0.0.1

Yesterday i was trying to connect to MySQL localhost with another port aside from 3306:

mysql -uroot -p -h localhost --port=3390

to my suprise i was login to the default port. My collegue told me that i need to used 127.0.0.1 instead of localhost so i tried it then i search everywhere to find the answer on why oh why its like this until i found a blog that explained the difference between localhost and 127.0.0.1.

source: http://blogs.sakienvirotech.com/index.php/random/2011/08/30/mysql-101-connecting-to-a

With network connections, there are two special addresses that always refer to the machine you are currently using. This is either called 'localhost' or has the IP address of 127.0.0.1.  In general using these is equivalent and interchangeable, and uses a special path through the networking subsystem called the local loopback interface. However MySQL has reserved 'localhost' for the special purpose of indicating the use of a UNIX socket and not use networking.  This can cause some confusion to new users, but if you understand what is going on it is pretty clear.
'localhost' in MySQL uses the UNIX socket and ignores any port you may supply.
127.0.0.1 in MySQL uses networking and requires a port, using the default if you don't supply it.



The On Demand Global Workforce - oDesk
Google