Connection to MySQL DB Failing in Terminal with "No such file or directory" Error
When attempting to establish a connection to MySQL via the Terminal on an Apple device using the PHP script provided, users may encounter the error "mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock)". This issue arises despite the script functioning properly when invoked through a browser.
Resolution
The underlying problem stems from macOS incorrectly identifying the location of the required socket file. To rectify this, follow these steps:
ls -l /tmp/mysql.sock /var/mysql/mysql.sock
If /tmp/mysql.sock exists:
cd /var sudo mkdir mysql sudo chmod 755 mysql cd mysql sudo ln -s /tmp/mysql.sock mysql.sock
If /var/mysql/mysql.sock exists:
cd /tmp ln -s /var/mysql/mysql.sock mysql.sock
Once the symbolic link is established, the connection to the MySQL database should be successfully established.
The above is the detailed content of Why is my PHP MySQL connection failing in the terminal with a 'No such file or directory' error, but working in a browser?. For more information, please follow other related articles on the PHP Chinese website!