Bitten by a PHP / MySQL upgrade
#91 Henry, Monday, 21 March 2011 3:53 PM (Category: Linux)
(Tags: php mysql)

I updated the Slackware on my webserver, as I regularly do. After the upgrade, I always check to see if any problems have occurred. I loaded one of the complex webpages to make sure it was still working, and found that it wasn't.

The page was not displayed and all I got was the cryptic message that PHP could not connect to the database.

Oops. Better start troubleshooting before customers discover it and get upset about their websites not working.

First thing, check if MySQL is running. I used ps, and it was running. I used mysql on the command line to see if I could connect directly to MySQL and I could log in and do regular MySQL stuff. So the problem is not with MySQL. It must be with my PHP configuration.

I looked in my Apache error logs and could not see any error messages. I must be trapping the errors when I connect to the database. So I picked one of my simple websites and edited the database connection function to dump error messages to the screen. Aha, I get "Error: 2002 - No such file or directory".

Time for Google.

Google leads to StackOverflow and the question PHP - MySQL connection not working: 2002 No such file or directory. The question was originally asked in November 2009, so this sort of problem has occurred before and there are good suggestions for fixing it.

It boils down to this. MySQL stores its socket details in /var/run/mysql/mysql.sock. Maybe it always did, or maybe it just changed to this location. PHP was expecting it in what it thought was its default location, and I think that was /tmp/mysql.sock. Because of this mismatch, PHP could not connect to MySQL.

I edited /etc/httpd/php.ini and changed this line to have the absolute location.

mysql.default_socket = /var/run/mysql/mysql.sock

Just in case PHP needs to reload the php.ini file, I restarted Apache. Then I ran my tests again and it was all working again. The whole thing didn't take more than 10 minutes to trouble-shoot and fix thanks to Google and StackOverflow.

0 comments