Image of Cortney & Jeremy

CakePHP on OS X can’t connect to MySQL

by Jeremy L. Gaddis on January 5, 2008 · 6 comments

in Open Source

If you’re trying to use CakePHP on OS X with a MySQL database, you’ll probably have problems connecting to the database server (by default).

In my case, I created the MySQL database using the MySQL command-line client, created a MySQL user for the application, gave it the appropriate permissions, configured CakePHP’s database.php configuration file, and tried to access the default page. I was presented with the following error:

Warning: mysql_connect() [function.mysql-connect]: Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (2) in /Library/WebServer/Documents/application_name/cake/libs/model/dbo/dbo_mysql.php on line 100

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Library/WebServer/Documents/application_name/cake/libs/model/dbo/dbo_mysql.php on line 105

Fortunately, the fix for this is pretty easy. I’ve seen recommendations to just make a symbolic link from /var/mysql/mysql.sock to /tmp/mysql.sock but that’s not really a good idea. Instead, open up /etc/php.ini in your text editor, find the line that begins with “mysql.default_socket = ” and change the value to point to “/tmp/mysql.sock” instead (and make sure the line isn’t commented out). Save and exit, restart Apache (“apachectl restart”) and you should be good to go.

If you don’t have an /etc/php.ini file but you do have /etc/php.ini.default, simply copy the default file over (e.g. “cp /etc/php.ini.default /etc/php.ini”) and then make the above change.

{ 6 comments… read them below or add one }

Leave a Comment

Previous post:

Next post: