Home > Database > Mysql Tutorial > Why is my PHP MySQL connection failing in the terminal with a 'No such file or directory' error, but working in a browser?

Why is my PHP MySQL connection failing in the terminal with a 'No such file or directory' error, but working in a browser?

Mary-Kate Olsen
Release: 2024-12-24 11:42:11
Original
690 people have browsed it

Why is my PHP MySQL connection failing in the terminal with a

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:

  1. Determine the location of the socket file using the command:
ls -l /tmp/mysql.sock /var/mysql/mysql.sock
Copy after login
  1. Create a symbolic link to the socket file in the location where the application is searching.

If /tmp/mysql.sock exists:

cd /var
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo ln -s /tmp/mysql.sock mysql.sock
Copy after login

If /var/mysql/mysql.sock exists:

cd /tmp
ln -s /var/mysql/mysql.sock mysql.sock
Copy after login
  1. If necessary, grant permissions to create the directory and link by prefixing the commands with "sudo".

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template