In PHPMySQL, localhost and 127.0.01 are connected to the last section of Nginx and PHP-FPM permission security configuration about database connection failure.
When I configured WordPress earlier, I wanted to allow mysql to connect to tcp. google will get the answer:
define('DB_HOST', '127.0.0.1');
But at that time did not pay attention to the wp-config.php configuration, the original has a configuration:
define('DB_HOST', 'localhost');
Later, when I performed permission processing on the Discuz Forum, mysql also took the unix domain socket, so I wanted to see how to change it to tcp. at that time, I saw that it was configured with localhost, I always thought about whether to configure a port to force tcp.
After a long time, I still couldn't do it. I found the PHP code directly by reporting an error and finally located it on the mysql_connect () function.
After reading the document, I didn't see any problem. I changed the localhost to 127.0.0.1. I suddenly found that I was so excited that I felt quite puzzled.
It was initially thought that PHP had made special processing on localhost and directly resolved it to the mysql sock path.
Let's look back at the problem after completing the task today.
A PHP-FPM chroot isolation environment is still made locally, test script:
Root @ gentoo-local/var/www/test % cat index. php
Same as yesterday's case, if it is localhost, it will fail; localhost: 3306 will also fail; 127.0.0.1 will be OK.
Because you know the question, the search is getting closer and closer to the correct answer.
So I found this answer Warning: mysql_connect (): [2002] No such file or directory, which contains a sentence:
The reason is that "localhost" is a special name for the mysql driver making it use the unix socket to connect to mysql instead of the tcp socket.
This is because of the problem of mysql parsing localhost. It is different from my previous guess that it was resolved in PHP.
The local mysql command line client is tested. Normally, both of the following are OK (-h localhost is the default mode, 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, temporarily comment out this part. Naturally, localhost becomes meaningless. 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 it can be determined that the reason should be the mysql driver's parsing of the db host.
In addition, you can also see the communication of the connection process 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)
In terms of experience, localhost and 127.0.0.1 are equivalent. if the host is not an IP address, it will attempt to perform resolution operations. MySQL may consider that the unix domain socket is more efficient, so the localhost is processed by default.
However, this kind of processing is not obvious, or it's just a bit more difficult.
I searched this topic and found that there were a lot of people who encountered this problem. for example, the answer from StackOverflow above, there were close to 300 Up vote.
:(