Home >Database >Mysql Tutorial >Why Does My PHP MySQL Connection Fail with a 'No such file or directory' Unix Socket Error?
Unix Socket File Error in PHP MySQL Connection
When attempting to connect to a MySQL database through the Terminal, you may encounter the following error:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
This error indicates that the PHP script cannot find the correct Unix socket file to connect to the MySQL server. The socket file, typically located at /tmp/mysql.sock or /var/mysql/mysql.sock, provides a communication channel between PHP and the MySQL server.
To resolve this issue, you can create a symbolic link between the actual socket file location and the expected location. This ensures that PHP can locate the socket file without encountering the error.
If you have /tmp/mysql.sock but no /var/mysql/mysql.sock, execute the following commands:
cd /var sudo mkdir mysql sudo chmod 755 mysql cd mysql sudo ln -s /tmp/mysql.sock mysql.sock
If you have /var/mysql/mysql.sock but no /tmp/mysql.sock, use these commands:
cd /tmp ln -s /var/mysql/mysql.sock mysql.sock
In either case, ensure that you have the necessary permissions to create the directory and symbolic link. If needed, prefix the commands with sudo.
Once the symbolic link is established, PHP will be able to connect to the MySQL server through the expected socket file location, resolving the error.
The above is the detailed content of Why Does My PHP MySQL Connection Fail with a 'No such file or directory' Unix Socket Error?. For more information, please follow other related articles on the PHP Chinese website!