Home > Article > Operation and Maintenance > centos cannot connect to the database using localhost
Question:
Under Centos system, use mysql database. When the host name is 127.0.0.1, the connection can be successful, but if localhost is used, the connection will fail.
Analysis:
There are two options for connecting to the mysql database.
1. TCP/IP (network connection)
TCP/IP socket connection method is a connection method provided by MySQL on any platform, and it is also the most commonly used one on the network. Way. This method establishes a network-based connection request on a TCP/IP connection. Generally, the client is on one server and the MySQL instance is on another server. The two machines are connected through the TCP/IP network
2. Unix domain socket connection (non-network connection)
In Linux and Unix environments, you can also use Unix domain socket connection. Unix domain sockets are not actually a network protocol, so they can only be used when the MySQL client and database instance are on the same server. The socket file path can be specified in the configuration file, such as -socket=/tmp/mysql.sock.
(Recommended tutorial: centos usage tutorial)
Solution:
1. Check the socket file location of the local mysql
mysqld --verbose --help | grep socket
2. Find a similar item in the query results
socket /var/run/mysqld/mysqld.sock
3. In the php configuration file, find the following item:
mysql.default_socket =
4. Modify it to :
mysql.default_socket = 找到的socket路径
5. Save the file and restart php-fpm
Recommended related video tutorials: linux video tutorial
The above is the detailed content of centos cannot connect to the database using localhost. For more information, please follow other related articles on the PHP Chinese website!