In my last post I discuss how to enable PHP over Apache in Mac OS X lion and today I bumped into a new problem. The problem is that when you install MySQL on Mac OS X and try to connect it via PHP, you will get an error. This error is thrown due to the fact that the path of the MySQL sockets are different in default php.ini and MySQL ‘s default configuration file.

So in this post, I am going to discuss how to solve this problem. Technically there are two ways to go about it. One way is to edit PHP’s default configuration to match that of the MySQL ‘s configuration and second way is the other way around i.e. changing MySQL ‘s configuration to match the PHP’s information file. However, Apple’s website suggest to change the PHP’s configuration.

By default if you connect, you will get the following PHP error –

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

The above error, clearly says that the socket file is missing or not found. So let’s see how we can resolve this –

Change location of mysql.sock in php.ini

      1. If you do not have the php.ini in the /etc/ folder then. Create one from default.php.ini which should be located at the same location.
      2. Open php.ini in your favorite editor . If you are going to edit it from terminal then use sudo vi /etc/php.ini
      3. When you are in file search for the string mysql.sock . You will see lines similar to the following.


        mysql.default_socket = /var/mysql/mysql.sock
        mysqli.default_socket = /var/mysql/mysql.sock


      4. As you can see the default path for all three sockets is /var/mysql which is there as the default php configuration. However, OS X has changed it to /tmp/
      5. So all you have to do is replace /var/mysql/mysql.soc to /tmp/mysql.sock
      6. Save the file and restart the server.
      7. To restart Apache server, go to terminal and type sudo apachectl restart
      8. After you restart the server, your PHP files creating database connection should work without any error.

This is preferred way of doing it. However, there is another alternate as discussed above. Next we will see how we can change the path of MySQL ‘s socket file instead of changing the php configuration file.

Change the location mysql.sock in my.cnf

If you are going to follow this method, then please do not make any changes in php.ini as mentioned above.

  1. First of all create the my.cnf  file , if it’s already not there in the /etc/ folder.
  2. Next, open this file in your favorite text editor and type the following two lines-


  3. Save this file and restart the MySQL server. To restart server type the following commands –

    sudo /usr/local/mysql/support-files/mysql.server stop
    sudo /usr/local/mysql/support-files/mysql.server start

  4. Now you should be able to run your PHP file containing database connection as usual. Note you are not required to restart apache server in this case. Since it’s already looking for the mysql.sock at the correct place.

This should solve your old scripts from oozing out database errors and save you from scratching your head for hours. Hope this helps.

Stay Digified!!
Sachin Khosla

Share this post: