Make PHP and MySQL communicate on Mac OS X 10.6 Snow Leopard

I write this blog post because every few months I need make PHP and MySQL work on Mac OS X and every time I have to figure it all out again. Time to write it down…

The following instructions consider Mac OS X 10.5 and 10.6 (Leopard and Snow Leopard).

Almost always people start shouting “MAMP!” as soon as people have problems with PHP and MySQL on Mac OS X. It is nonsense to install MAMP on a Mac because almost everything is already installed on your Mac. Why would you install all necessary server software twice? It just takes away hard disk space. You really don’t need to use MAMP. All you need is to figure out how to configure PHP and MySQL.

Obviously, before PHP can work with MySQL, you need to install MySQL. You can download MySQL for Mac OS X here . Install MySQL and don’t forget to install the preferences panel that comes with it.

Now you can activate PHP. Type command-shift-G in the Finder and type /etc/apache2/ in the little window that appears. Click on Go en find the file httpd.conf in that folder. Copy this file to the desktop and create a backup copy named httpd.conf.old.

Open the file httpd.conf in TextEdit and search for the line containing

#LoadModule php5_module libexec/httpd/

Remove the # and save the file. Copy the modified file to /etc/apache2/ en cofirm if you are asked whether you want to replace the existing file.

When PHP tried to communicate with MySQL, it uses a socket file to contact MySQL. PHP expects o find this file in /tmp/mysql.sock while MySQL puts the file into /var/mysql/mysql.sock. It has been like this for year and I really don’t understand why the two parties involved don’t solve this.

The “temporary”solution is to tell either PHP or MySQL to create or search the socket file somewhere else. The easiest way (in my personal opinion) is the following.

1) open TextEdit and type the following four lines:


2) Save this text file at the location: /etc/my.cnf

You may be unable to save the file in /etc right-away. In this case, you can save the file on your desktop first. Type command-shift-G (in the Finder) and type /etc in the little window that appears. If you click on Go, you will see a Finder window displaying the content of folder /etc. Now you can copy the file my.cnf to the folder /etc. You may need to enter your admin name and password when you copy the file.

Now you need to turn off and on Web Sharing in the System Preferences. You may need to turn off and on MySQL too, which you can do in the MySQL preferences panel.

PHP should now be able to use MySQL.

Leave a Reply

Your email address will not be published. Required fields are marked *