The difference between MySQL database host 127.0. 0.1 and localhost

  • 2021-10-25 00:05:50
  • OfStack

There may be many friends who will encounter a problem and don't know what is the difference between 127.0. 0.1 and localhost, but sometimes they will find that they can't connect with localhost, but they can connect with 127.0. 0.1, so what is the difference between them? Let me introduce it to you below.

For mysql-h 127.0. 0.1, the TCP/IP connection is used, and mysql server considers the connection to be from 127.0. 0.1 or "localhost. localdomain." When mysql-h localhost, TCP/IP connection is not used, but Unix socket;; At this point, mysql server considers the client to be from "localhost." "localhost" in mysql rights management has a specific meaning:

Note: Although there are differences between the two connection methods, 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 records are matched first)


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

Proof:


shell> mysql -h 127.0.0.1 
mysql> status; 
Currentuser: root@localhost 
SSL: Notin use 
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: Notin use 
Current pager: stdout 
Using outfile: '' 
Using delimiter: ; 
Server version: 5.1.33-log Source distribution 
Protocol version: 10 
Connection: Localhost via UNIX socket

Find a problem

Yesterday, when I helped my colleagues compile and install Linux environment, I encountered a problem:

The Web server is apache and the database is MySQL.

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


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

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

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

Check that the environment is normal

Thought that the database did not start, so look at the process under 1, MySQL in the process, restarted under 1 MySQL.

Use mysql -u root -p You can enter the MySQL operation interface

Direct use /usr/local/php5/bin/php /web/test.php Execution can connect to the database

apache has also been restarted, and one sample is invalid

Doubt: Why did the web page fail and the command succeed

This is depressing, using the php command to execute directly on the success, through the web page to execute on the failure. Is it caused by apache? After searching a lot of information on the Internet, no solution was found, and the problem of recompiling and installing apache remained.

Successfully changed localhost to 127.0. 0.1

After changing localhost to 127.0. 0.1, the connection succeeded, and began to fall into a thinking dilemma: localhost failed but 127.0. 0.1 succeeded?

ping localhost Address 127.0. 0.1 Yes

Open hosts Join

127.0.0.1 qttc

When qttc is used, the host connection is normal, but localhost is not recognized.

Different localhost connection modes lead to

In order to understand the PHP connection to the database, the host filled in localhost and other differences read a lot of information, and finally learned:

mysql will be used when the host is filled in as localhost unix domain socket Connect

When the host is 127.0. 0.1, mysql will be connected by tcp

This is a feature of the linux socket network, and the win platform does not have this problem

Solution

Add in the [mysql] section of my. cnf

protocol=tcp

Summarize


Related articles: