Does localhost and 127.0.0.1 differ when accessing the MySQL database?

Source: Internet
Author: User
Access the local MySQL database in PHP with the following code:

$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 written localhost , the records in the library table can be displayed as normal. And when the host name is written 127.0.0.1 , although the connection database did not report error, but reported QUERY ERROR . Is that localhost so 127.0.0.1 different? What's going on here?

Reply content:

Access the local MySQL database in PHP with the following code:

$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 written localhost , the records in the library table can be displayed as normal. And when the host name is written 127.0.0.1 , although the connection database did not report error, but reported QUERY ERROR . Is that localhost so 127.0.0.1 different? What's going on here?

Loaclhost and 127.0.0.1 are not equivalent.

Loaclhost is a name, this name generally represents the 127.0.0.1 IP address , that is, when you say to the computer, I want to go to Loaclhost, The computer translates it into 127.0.0.1 via DNS or NBNS or the hosts file.

If you use Loaclhost to link to a database, MySQL uses Unixsocket to link to it. If 127.0.0.1 is specified, TCP/IP is used to link.

So try to use 127.0.0.1.

Well, see the answer has been adopted, but still have to say a little more

In fact, the answer is not very rigorous, but since it was adopted, say a little more. localhostin general, and 127.0.0.1 is equivalent, but MySQL in the problem of dealing with this noun there are some differences, is based on different addresses to take different means of communication.

The reason, I guess, is that the local application can get better performance. And localhost this address does not match in MySQL. That user@'%' cannot be matched touser@'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.