localhost and 127.0.01 in the Php/mysql

Source: Internet
Author: User
Tags mysql connect unix domain socket
Next to the Nginx and PHP-FPM permissions Security configuration Last paragraph about database connection failure processing.

Before the configuration of WordPress, then want to let MySQL connect to go TCP, Google will get the answer:

Define (' Db_host ', ' 127.0.0.1 ');

However, at that time did not pay attention to the wp-config.php configuration, originally had a configuration:

Define (' db_host ', ' localhost ');

Later in the Discuz forum to do permissions processing, because MySQL is also the UNIX domain socket, so want to see how to go TCP, then see is equipped with localhost, always think that is not equipped with a port up on the forced to go TCP.

Toss for a while, or not, directly through the error location of the PHP code, the final positioning to the mysql_connect () function.

Read the document also did not see where there is a problem, and then change the localhost to 127.0.0.1, suddenly found the line, excited also feel very inexplicable.

The first thought is that PHP has a special handling of localhost, directly resolved to the MySQL sock path.

Take a look back at the problem today after tossing the matter.

Still has a local PHP-FPM chroot isolation environment, test scripts:

Root@gentoo-local/var/www/test% Cat index.php
 
  
 
  

As in yesterday's case, if it is localhost, it fails; localhost:3306 also failed; 127.0.0.1 is OK.

Because we know what the problem is, the search is getting closer to the right answer.

So I found this answer warning:mysql_connect (): [2002] No such file or directory, there is a sentence:

The reason is this "localhost" is a special name for the MySQL driver making it with the UNIX socket to connect to MySQL in Stead of the a TCP socket.

This is because MySQL has an analytic problem with localhost. And I guess before is the inconsistency of PHP parsing.

Local MySQL command-line client test, under normal circumstances both OK (-h localhost is the default, not specified):

$ mysql-h localhost-u root-p   # ok$ mysql-h 127.0.0.1-u root-p   # OK

Because localhost is defined in/etc/hosts, so temporarily commenting out this block, natural localhost becomes meaningless string, try again:

$ mysql-h localhost-u root-p   # ok$ mysql-h 127.0.0.1-u root-p   # OK

The two are still OK, so you can be sure that the reason for this is MySQL driver's parsing of DB host.

In addition, the communication of the connection process can also be seen through Lsof:

# localhostmysql 27971 tankywoo  3u  Unix 0xffff880078d25540  0t0 371911 type=stream#  127.0.0.1mysql 28249 tankywoo  3u  IPv4 373797       0t0  TCP Localhost:50187->localhost:mysql (established)

According to experience, localhost and 127.0.0.1 are equivalent, if the host is a non-IP address, will try to do parsing operations. MySQL may be taking into account that UNIX domain sockets are more efficient, so this process is done by default on localhost.

But this treatment is not obvious, or ambiguous, so I feel that the "pit" of the nature of a larger.

Follow-up against this block and searched, found that there are many people encounter this problem, such as the above StackOverflow answer, there are nearly 300 of the up vote can be seen.

:(

  • 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.