It perfectly solves the problem that MySQL cannot connect to the database through localhost, mysqllocalhost
Problem:A server's PHP program cannot connect to the database through the localhost address, but if it is set to 127.0.0.1, the connection can be normal, and the connection to other database servers is normal. The MySQL permission is correctly set and the mysql command line client can connect to the database normally.
Analysis:This is a typical case where the socket is not correctly set.
There are two ways to connect to the MySQL database: TCP/IP (the port that is generally understood) and Unix socket (usually socket or sock ). In most cases, you can use localhost to represent the local machine 127.0.0.1. However, the two cannot be mixed during MySQL connection, and localhost and 127.0.0.1 in MySQL permission settings are also set separately. When it is set to 127.0.0.1, the system connects to the database through TCP/IP; when it is set to localhost, the system connects to the database through socket.
Solution: First, check where the socket file of MySQL is located. The command is as follows:
mysqld --verbose --help | grep socket
The output result shows the location of the socket file. For example, this server displays
socket /var/run/mysqld/mysqld.sock
Then modify the php configuration file php. ini to match it.
Find this item:
mysql.default_socket =
Generally, this item is empty and changed:
mysql.default_socket = /var/run/mysqld/mysqld.sock
Here, you should write the file found in the previous step and set it according to your situation. Now the php configuration has been modified. If it is CLI (command line) or CGI, it will take effect immediately. If it is FASTCGI, restart the fastcgi process.
The above is a perfect solution to the problem that MySQL cannot connect to the database through localhost, that is, all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's house.