After installing Mac OS X Lion, I found the PHP site I use for development stopped working. I’d just get an empty webpage when I went to the website. It’s a WordPress site, using both PHP and MySQL. Lion ships with PHP 5.3.6, the latest production release of PHP.
In WordPress’s wp-config.php file, I set the WP_DEBUG constant to true to see what is going on:
It turns out WordPress / PHP could not connect to the MySQL database on my Mac running Lion:
Warning: mysql_connect() [function.mysql-connect]:  No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/taz/Sites/smartwebdeveloper/wp-includes/wp-db.php on line 1036
I checked my Apache config. The Lion upgrade process left my Apache config alone, so my virtual host was still intact and working. It put a new httpd.conf.default in place in case I Apache wanted to use an more up to date Apache configuration. Happily it had left my configuration that enabled the PHP 5 module in place.
I remembered that Mac OS X Server’s MySQL install puts the socket for communicating with MySQL at /var/mysql/mysql.sock. The install of MySQL I’m using, from mysql.com, in order to not conflict with Apple’s MySQL, puts the socket at /tmp/mysql.sock. I’d configured PHP back when I was using Snow Leopard to talk to the MySQL socket in /tmp.
I created a quick PHP file calling the phpinfo() function in my website’s home directory to check the PHP configuration:
I went to that webpage through the web and found, sure enough that all three PHP MySQL extensions – MySQL, MySQLi and PDO MySQL were all expecting the MySQL socket to be in /var/mysql.
I checked the PHP configuration directory /etc/php. The reason why PHP wasn’t working properly (i.e. connecting to MySQL) was OS X Lion install had moved my PHP configuration file – php.ini – out of the way:
tazair:smartwebdeveloper taz$ ls -l /etc/php* -rw-r--r-- 1 root wheel 69302 20 Feb 00:42 /etc/php.ini-5.2-previous -r--r--r-- 1 root wheel 69337 21 Jul 12:20 /etc/php.ini.default -r--r--r-- 1 root wheel 69060 16 Dec 2010 /etc/php.ini.default-5.2-previous
Lion renamed my php.ini file to /etc/php.ini-5.2-previous, and put a new sample PHP configuration file, /etc/php.ini.default. It makes sense to do this: Lion uses PHP 5.3 which has new options and defaults, compared to PHP 5.2 used on Snow Leopard.
I copied the example PHP configuration into place:
sudo cp /etc/php.ini.default /etc/php.ini
I edited the new PHP configuration to update all references to /var/mysql/mysql.sock to /tmp/mysql.sock. Before editing php.ini:
tazair:~ taz$ grep .default_socket /etc/php.ini pdo_mysql.default_socket=/var/mysql/mysql.sock mysql.default_socket = /var/mysql/mysql.sock mysqli.default_socket = /var/mysql/mysql.sock
After editing php.ini:
tazair:~ taz$ grep .default_socket /etc/php.ini pdo_mysql.default_socket=/tmp/mysql.sock mysql.default_socket = /tmp/mysql.sock mysqli.default_socket = /tmp/mysql.sock
I also enabled two of the three MySQL extensions, which were configured off in the default php.ini in the Lion install. WordPress perhaps only needed one of three MySQL extensions:
tazair:~ taz$ grep mysql /etc/php.ini|grep ext extension=php_mysql.dll extension=php_mysqli.dll ;extension=php_pdo_mysql.dll
I needed to restart Apache for it to pickup the new configuration:
tazair:~ taz$ sudo apachectl restart
Happy days! PHP could talk to MySQL, and my WordPress site was working on OSX Lion.
If you have any questions or comments about the process above, please leave a comment. If this article helped you, please click the Tweet or +1 buttons. Thanks for visiting!