Error: Unable to Establish MySQL Connection through Unix Socket
Upon attempting to connect to your MySQL database via the Terminal on your Mac using PHP, you 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 an issue with the MySQL socket file, which is used for UNIX-based connections to the database.
Solution: Create a Symbolic Link
The root cause of this issue is a discrepancy in the socket file's location on macOS. To resolve it, you need to create a symbolic link, which is a file that points to another file. This will allow your Mac to find the required socket file even if it's looking in the wrong place.
Steps:
Check Socket File Location
Use the following command to determine the location of the socket file:
ls -l /tmp/mysql.sock /var/mysql/mysql.sock
Create Symbolic Link
Depending on the location of the socket file, you will need to create a symbolic link as follows:
cd /var sudo mkdir mysql sudo chmod 755 mysql cd mysql sudo ln -s /tmp/mysql.sock mysql.sock
cd /tmp ln -s /var/mysql/mysql.sock mysql.sock
Note: You may need to use sudo before the commands to gain sufficient permissions.
The above is the detailed content of Why is my PHP MySQL connection failing 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!