cakephp on os x can’t connect to mysql
Written by jlgaddis on January 5, 2008 – 11:06 pm -just a heads up… 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 (and 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
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 (as root), find the line that begins with “mysql.default_socket = ” and change the entire line so that it reads “mysql.default_socket = /tmp/mysql.sock” (make sure there’s not a semicolon at the beginning of the line). save the file, exit, and restart apache (“apachectl restart”) and you should be good to go.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
note that 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”), then do the fix above.
Tags: apple, osx, software | 6 Comments »




March 26th, 2008 at 5:25 am
Thanks a lot for the help!
Just what I needed!
May 3rd, 2008 at 8:11 pm
excellent, glad i be of some help!
August 13th, 2008 at 3:56 pm
hey!!! perfect! works ok!
sos un capo!
my thanks… from argentina
August 13th, 2008 at 4:08 pm
Thanks a lot
August 31st, 2008 at 9:47 pm
If you are using xampp rather than the Apple webserver, you will need to tell cake (and the bash shell generally) to use that php installation instead of the apple one. Instead of editing your php.ini, just enter this line in your shall before calling cake:
export PATH=/Applications/xampp/xamppfiles/bin:$PATH
If you place that line in your bash .profile, you will now normally execute php scripts (like cake) using the xampp php installation instead of the Apple one.
If you are using mamp or have xampp installed in a non-standard place, adjust your path/to/php accordingly. Obviously if you are using tcsh or another shell, you’ll also have to adapt these instructions to how you set environment variables in another shell.
March 29th, 2009 at 11:05 pm
Thanks mate, Was very helpful in a moment of need.
Cheers…