Home >Database >Mysql Tutorial >How to Fix the 'Warning: mysql_connect(): [2002] No such file or directory' Error in PHP?
Troubleshooting "Warning: mysql_connect(): [2002] No such file or directory" Error
This error commonly occurs when attempting to connect to a MySQL database using PHP's mysql_connect() function. While the connection may work fine through a web browser, it fails when executed through the Terminal.
In this case, the issue arises due to a discrepancy in the location of a required socket file. By default, macOS stores the socket file in either /tmp/mysql.sock or /var/mysql/mysql.sock. However, PHP may be looking in the incorrect location.
To resolve this, create a symbolic link between the two locations to ensure that PHP can find the socket file regardless of where it expects to find it.
For systems with /tmp/mysql.sock but no /var/mysql/mysql.sock:
cd /var sudo mkdir mysql sudo chmod 755 mysql cd mysql sudo ln -s /tmp/mysql.sock mysql.sock
For systems with /var/mysql/mysql.sock but no /tmp/mysql.sock:
cd /tmp ln -s /var/mysql/mysql.sock mysql.sock
Executing these commands as a privileged user (e.g., with sudo) will create the necessary directory and link. Once the symbolic link is established, PHP should be able to successfully connect to the MySQL database through the Terminal.
The above is the detailed content of How to Fix the 'Warning: mysql_connect(): [2002] No such file or directory' Error in PHP?. For more information, please follow other related articles on the PHP Chinese website!