When accessing the mysql database, is localhost different from 127.0.0.1?

Source: Internet
Author: User
Use php to access the local mysql database. the code is as follows: {code...} when the host name is localhost, the records in the database table can be displayed normally. When the host name is written as 127.0.0.1, QUERYERROR is reported even though no error is reported when the database is connected. In this case, localhost... uses php to access the local mysql database. the code is as follows:

$mysqli = new mysqli("localhost", $name, $passwd, "test");if ($mysqli->connect_errno) {  die("Failed to Connect to mysql: " . $mysqli->connect_errno);}$result = $mysqli->query("SELECT * FROM pet;");if(!$result){  echo "QUERY ERROR: " . $mysqli->error;}... ...$mysqli->close();

When the host name is writtenlocalhostThe records in the database table can be displayed normally. When the host name is written127.0.0.1Even if no error is reported when you connect to the databaseQUERY ERROR. So FarlocalhostAnd127.0.0.1Are they different? What's going on?

Reply content:

Use php to access the local mysql database. the code is as follows:

$mysqli = new mysqli("localhost", $name, $passwd, "test");if ($mysqli->connect_errno) {  die("Failed to Connect to mysql: " . $mysqli->connect_errno);}$result = $mysqli->query("SELECT * FROM pet;");if(!$result){  echo "QUERY ERROR: " . $mysqli->error;}... ...$mysqli->close();

When the host name is writtenlocalhostThe records in the database table can be displayed normally. When the host name is written127.0.0.1Even if no error is reported when you connect to the databaseQUERY ERROR. So FarlocalhostAnd127.0.0.1Are they different? What's going on?

Loaclhost and 127.0.0.1 cannot be equivalent.

Loaclhost isNameThis name generally represents 127.0.0.1IP addressThat is to say, when you say to the computer that I want to go to loaclhost, the computer will translate it into 127.0.0.1 through the DNS, NBNS, or hosts file.

If you use loaclhost to connect to the database, Mysql uses unixsocket to link. If this parameter is set to 127.0.0.1, TCP/IP is used for the link.

Therefore, try to use 127.0.0.1.

Well, I can see that the answer has been adopted, but I 'd like to say a little more.

In fact, the answer is not very rigorous, but since it has been adopted, let's say a little more. GenerallylocalhostAnd127.0.0.1It is equivalent, but mysql has some differences in terms of processing this term. it uses different communication methods based on different addresses.

The reason is, I guess it is probably for local applications to achieve better performance. AndlocalhostThis address does not match in mysql. That isuser@'%'Cannot matchuser@'localhost'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.