Home > Backend Development > PHP Tutorial > Why is my PHP MySQL connection failing with 'No such file or directory' and how can I fix it using symbolic links?

Why is my PHP MySQL connection failing with 'No such file or directory' and how can I fix it using symbolic links?

Barbara Streisand
Release: 2024-12-09 01:56:11
Original
925 people have browsed it

Why is my PHP MySQL connection failing with

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
Copy after login

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
Copy after login

Create Symbolic Link

Depending on the location of the socket file, you will need to create a symbolic link as follows:

  • If you have /tmp/mysql.sock but not /var/mysql/mysql.sock:
cd /var 
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo ln -s /tmp/mysql.sock mysql.sock
Copy after login
  • If you have /var/mysql/mysql.sock but not /tmp/mysql.sock:
cd /tmp
ln -s /var/mysql/mysql.sock mysql.sock
Copy after login

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!

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