Home >Backend Development >PHP Tutorial >Why Does My MySQL Connection Fail with 'No Such File or Directory' in the Terminal?
Connecting via Unix Socket: Resolving "No Such File or Directory" Error
Encountering the error "Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in" when attempting to connect to a MySQL database from the Terminal can be perplexing. While the script may function properly when executed from a browser, the Terminal encounters issues.
Understanding the Issue
On macOS, MySQL configurations can sometimes misidentify the location of the socket file. The socket may exist in either /tmp/mysql.sock or /var/mysql/mysql.sock, but applications may search for it in the incorrect location.
Resolving the Error
Thankfully, the solution is straightforward and involves setting up a symbolic link. If /tmp/mysql.sock is present but /var/mysql/mysql.sock is absent, execute the following commands:
cd /var sudo mkdir mysql sudo chmod 755 mysql cd mysql sudo ln -s /tmp/mysql.sock mysql.sock
Conversely, if /var/mysql/mysql.sock exists but /tmp/mysql.sock does not, use these commands:
cd /tmp ln -s /var/mysql/mysql.sock mysql.sock
By creating a symbolic link, the Mac can find the required socket, regardless of which location it is searching in.
The above is the detailed content of Why Does My MySQL Connection Fail with 'No Such File or Directory' in the Terminal?. For more information, please follow other related articles on the PHP Chinese website!