PHP links MySQL host 127.0.0.1 and localhost
PHP links MySQL host 127.0.0.1 and localhost
Use mysql-u root-p to enter the MySQL operation interface
You can directly use/usr/local/php5/bin/php/web/test. php to connect to the database.
Apache is also restarted, which is also invalid.
Question: Why does the web page fail to be executed but the command is successfully executed?
This is depressing. If you use the php Command to directly execute the command, the execution will fail through the web page. Is it caused by apache? No solution was found after searching a bunch of materials on the Internet, and the problem of re-Compiling and installing apache still persists.
Changed localhost to 127.0.0.1.
After I changed localhost to 127.0.0.1, the connection was successful and I began to think about the dilemma: Why did the localhost fail 127.0.0.1 but it succeeded?
The ping localhost address is 127.0.0.1.
Open hosts to join
127.0.0.1 qttc
When qttc is used, the host connection is normal, but localhost is not recognized.
Due to different localhost connection methods
To learn about the differences between localhost and other settings when using PHP to connect to the database, I read a lot of information and finally learned:
When the host is set to localhost, mysql uses unix domain socket connection.
Mysql uses tcp connection when the host is set to 127.0.0.1.
This is a feature of the linux Socket network, and the win platform does not have this problem.
Solution
In the [mysql] section of my. cnf, add
Protocol = tcp
Save and restart MySQL. The problem is solved!