Archive for July 4, 2007
What is the difference between localhost and localhost.localdomain on MySQL
This is one of the things that confused me for a long time because it isn’t mentioned in a lot of MySQL documentation. In MySQL in Linux, you can do the following:
mysql -u root -p
select user, host from mysql.user;
You may get the following output returned:
+------+-----------------------+
| user | host |
+------+-----------------------+
| | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
4 rows in set (0.00 sec)
What is the differences between localhost and localhost.localdomain? Both localhost and localhost.localdomain refers to the local computer. The difference is as followed:
- The host localhost allows connection to mysql through the UNIX socket.
- The host localhost.localdomain allows connection to mysql through TCP/IP.
When you type mysql and don’t specify a host, it will connect via UNIX socket. If you type mysql -h localhost.localdomain, it will connect via TCP/IP. You can check which which connection you are at when you type the command status. The line connection will list either UNIX socket or TCP/IP.
Connecting through the UNIX socket has a lower overhead than through TCP/IP, but not every client can connect to a UNIX socket.