Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between host 127.0.0.1 and localhost in MySQL

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you what is the difference between host 127.0.0.1 and localhost in MySQL. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

When mysql-h 127.0.0.1 uses a TCP/IP connection, mysql server thinks the connection comes from 127.0.0.1 or "localhost.localdomain"

When mysql-h localhost is not connected using TCP/IP, while using Unix socket;, mysql server thinks that the client is from "localhost"

The word "localhost" in mysql rights management has a specific meaning:

Note: although there is a difference between the two connection methods, when localhost is 127.0.0.1 by default, 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 Discovery problem

Yesterday, I encountered a problem while compiling and installing the Linux environment for my colleagues:

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 the http://localhost/test.php test

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

Check that the environment is normal

Thought that the database did not start, so check the process, MySQL in the process, restart MySQL.

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

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

Apache has also been restarted, which is also invalid.

Doubt: why the execution of the web page failed, but the execution of the command succeeded

This is depressing, the direct execution of the php command is successful, and the execution through the web page fails. Is it caused by apache? After searching a lot of information on the Internet, I couldn't find a solution, and the problem of recompiling and installing apache is still the same.

Change localhost to 127.0.0.1 successfully

After changing localhost to 127.0.0.1, the connection was successful and began to fall into a thinking dilemma: localhost failed 127.0.0.1 but succeeded?

The ping localhost address is 127.0.0.1, yes.

Open hosts to join

127.0.0.1 qttc

Use qttc when the host connection is also normal, but do not recognize localhost.

Different ways of localhost connection result in

In order to understand that when PHP connects to the database, the host reads a lot of materials by filling in localhost and other differences, and * knows:

When the host is localhost, mysql will use unix domain socket connection.

When the host is 127.0.0.1, mysql will connect in tcp mode.

This is a feature of linux socket networks, and win platforms will not have this problem.

Solution method

Add in the [mysql] section of my.cnf

The above content of protocol=tcp is what is the difference between host 127.0.0.1 and localhost in MySQL. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report