mysql_connect and ‘No such file or directory’

June 2, 2010

I was working with a friend to get him started with PHP development on OSX, and we were stumped by a ‘No such file or directory’ error message for a long time.  It seemed to be coming from the following line:

$this->linkid = @mysql_pconnect($host, $user, $password) or die(mysql_error());

At first, we didn’t understand it, because it doesn’t look like PHP is trying to connect to a file there.  However, we eventually discovered that the file that wasn’t found was the MySQL socket.  Because he had used Ports to install MySQL instead of downloading the .dmg from mysql.com, he didn’t have the same default socket as most other people on the internet.

If you are also getting this error, follow the steps below to diagnose and fix it.

  1. Narrow down error to mysql_connect() or mysql_pconnect().
  2. Make a phpinfo() page.  Look for ‘mysql.default_socket‘, ‘mysqli.default_socket‘, and ‘pdo_mysql.default_socket‘.  Remember their value; this is where PHP is trying to connect to MySQL.
  3. Start mysql.  Execute ‘STATUS;‘ and look for the ‘UNIX socket‘ value.  If doesn’t match the value from phpinfo(), that’s the culprit.
  4. Open your php.ini file (which is also found on your phpinfo() page as ‘Loaded Configuration File‘) and change all the occurrences of the incorrect socket location to the correct socket location from MySQL.
  5. Restart Apache (on OSX: sudo apachectl restart) and try reloading your page.
Advertisement

9 Responses to “mysql_connect and ‘No such file or directory’”

  1. Ben Says:

    Nice one. I had this exact problem and you had the exact fix! :)

  2. Mike Says:

    Awesome, I’m happy I could save someone the time it took us to figure it out!

  3. Alexis Says:

    Wow, thanks! This worked just like you said. There’s something weird though, I have phpmyadmin at localhost, and even before, and after I modified the php.ini file, it worked flawlessly. All other local sites wouldn’t. The offending site began working as I changed the file to the correct settings. So I just cannot pinpoint exactly what happened!! Any clue?? Could it be that phpmyadmin has its own mysql socket configuration? Thanks again!

    • Mike Says:

      That is really strange. Unfortunately, I’ve never used phpmyadmin, so I really can’t help there. Having its own config file definitely sounds plausible, though!


  4. I came across this solution, but it took a while for me to navigate through all sorts of stuff just to implement it. Suffice it to say, that you are great at presenting this. I used to develop in Windows but am no longer in that world, thank god. I remember learning what autoexec.bat was(25 years ago), and I feel like I am in that world again with Unix(Mac). It’s fun to learn all over again. You solved a minor problem that would have taken me far longer. Now onto more learning.

  5. steve Says:

    Hi, changing ‘localhost’ with 127.0.0.1 also does the trick

    • Tiago Carvalho Says:

      Worked for me! Thanks!

  6. Kevin Smith Says:

    Great solution for many problems, but not all. Here’s a page that shows how to solve the problem if it’s due to the location of the socket file.

    http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi

    K

  7. Eric Says:

    http://www.yourethemannowdog.com/

    Seriously, thanks for posting this!


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.