"Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in" Issue: Localhost Refuses Connection
Despite encountering no issues previously, users may face the error "mysql_connect(): [2002] No such file or directory" while attempting to connect to their MySQL database via the Terminal. This issue stems from MySQL on OS X referencing incorrect socket file locations.
To resolve this, a symbolic link can be created to bridge the gap between the expected and actual socket file location.
Determining the Socket File Location
Execute the command ls -l /tmp/mysql.sock /var/mysql/mysql.sock to locate the socket file. The missing file will indicate the incorrect location.
Creating the Symbolic Link
For example, if the socket file is located at /tmp/mysql.sock:
Creating the Directory (if necessary):
cd /var sudo mkdir mysql sudo chmod 755 mysql
Creating the Symbolic Link:
cd mysql sudo ln -s /tmp/mysql.sock mysql.sock
After creating the symbolic link, you should be able to establish a connection to the database.
The above is the detailed content of Why Does My MySQL Connection Fail with 'No such file or directory' and How Can I Fix It Using Symbolic Links?. For more information, please follow other related articles on the PHP Chinese website!