The difference between MySQL database host 127.0.0.1 and localhost

The difference between MySQL database host 127.0.0.1 and localhost

Many of my friends may encounter a problem and don’t know what the difference is between 127.0.0.1 and localhost. However, sometimes they find that they cannot connect using localhost, but they can connect by changing to 127.0.0.1. So what is the difference between them? Let me introduce them to you.

  1. When you use mysql -h 127.0.0.1, you use TCP/IP to connect. MySQL server thinks that the connection comes from 127.0.0.1 or "localhost.localdomain".
  2. When you use mysql -h localhost, it does not use TCP/IP connection, but uses Unix socket; at this time, mysql server thinks that the client is from "localhost"
  3. "localhost" in MySQL permission management has a specific meaning:

Note: Although the two connection methods are different, when localhost is the default 127.0.0.1, the permission records used by both connection methods are the following 1.row records (because the record comes first and is matched first)

*************************** 1. row *************************** 
Host: localhost 
User: root 
...... 
*************************** 2. row *************************** 
Host: 127.0.0.1 
User: root

prove:

shell>mysql -h 127.0.0.1 
mysql> status; 
Currentuser: root@localhost 
SSL: Not used 
Current pager: stdout 
Using outfile: '' 
Using delimiter: ; 
Server version: 5.1.33-log Source distribution 
Protocol version: 10 
Connection: 127.0.0.1 via TCP/IP 
shell> mysql -h locahostmysql> status; 
Currentuser: root@localhost 
SSL: Not used 
Current pager: stdout 
Using outfile: '' 
Using delimiter: ; 
Server version: 5.1.33-log Source distribution 
Protocol version: 10 
Connection: Localhost via UNIX socket

Find the problem

Yesterday, when I was helping my colleague compile and install the Linux environment, I encountered a problem:

The web server is apache and the database is MySQL.

So I wrote a PHP page to test the connection to the database:

$mysql = mysql_connect('localhost','root','');

Open http://localhost/test.php to test

Tip: Can't connect to local MySQL server through socket...

Check that the environment is normal

I thought the database was not started, so I checked the process and found MySQL was in the process, so I restarted MySQL.

Use mysql -u root -p to enter the MySQL operation interface

Directly use /usr/local/php5/bin/php /web/test.php to execute and connect to the database

I restarted apache, but it didn't work.

Question: Why the web page execution failed, but the command execution succeeded

This is very frustrating. It succeeds when I execute it directly using the PHP command, but fails when I execute it through the web page. Is it caused by apache? I searched a lot of information online but couldn't find a solution. The problem still exists after recompiling and installing Apache.

Changing localhost to 127.0.0.1 succeeded

After changing localhost to 127.0.0.1, the connection was successful. I began to think in a dilemma: why did localhost fail but 127.0.0.1 succeed?

Ping localhost address is 127.0.0.1

Open hosts and join

127.0.0.1 qttc

Using qttc as the host connection is normal, but it does not recognize localhost.

Different localhost connection methods lead to

In order to understand the difference between filling in localhost and other hosts when PHP connects to the database, I read a lot of information and finally learned:

When the host is filled in as localhost, MySQL will use unix domain socket connection

When the host is filled in as 127.0.0.1, MySQL will use TCP to connect

This is a feature of the Linux socket network. Windows platforms will not have this problem.

Workaround

Add in the [mysql] section of my.cnf

protocol=tcp

Summarize

The above is the difference between MySQL database host 127.0.0.1 and localhost introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Perfect solution to the problem that MySQL cannot connect to the database through localhost
  • Solution to the problem that MySQL can connect using localhost but cannot connect using IP
  • The difference between mysql_connect localhost and 127.0.0.1 (network layer explanation)
  • How to solve the problem of unsuccessful connection to Mysql using localhost in PHP
  • php mysql localhost, 127.0.0.1 and ip difference
  • Does the % in the newly created MySQL user include localhost?

<<:  Detailed explanation of Nginx timed log cutting

>>:  Summarize the problems encountered in using Vue Element UI

Recommend

In-depth understanding of the vertical-align property and baseline issues in CSS

vertical-align attribute is mainly used to change...

MySQL multi-master and one-slave data backup method tutorial

Overview Operations on any one database are autom...

echars 3D map solution for custom colors of regions

Table of contents question extend Solving the pro...

The problem of form elements and prompt text not being aligned

Recent projects involve the creation of a lot of ...

Detailed explanation of JavaScript function this pointing problem

Table of contents 1. The direction of this in the...

Best Practices Guide for Storing Dates in MySQL

Table of contents Preface Do not use strings to s...

JavaScript Document Object Model DOM

Table of contents 1. JavaScript can change all HT...

5 Ways to Clear or Delete Large File Contents in Linux

Sometimes, while working with files in the Linux ...

Illustration of the process of using FileZilla to connect to the FTP server

When I first started setting up an ftp server on ...

MySQL Innodb key features insert buffer

Table of contents What is insert buffer? What are...

MySQL Series 14 MySQL High Availability Implementation

1. MHA ​By monitoring the master node, automatic ...

Research on the effect of page sidebar realized by JS

Table of contents Discover: Application of displa...