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      

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 

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.

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.

Share and Enjoy:
  • StumbleUpon
  • Digg
  • Reddit
  • Facebook
  • del.icio.us
  • Twitter

Tags: , , | 6 Comments »

6 Comments to “cakephp on os x can’t connect to mysql”

  1. Bob Kremmins Says:

    Thanks a lot for the help!

    Just what I needed!

  2. jeremy Says:

    excellent, glad i be of some help!

  3. maca Says:

    hey!!! perfect! works ok!

    sos un capo!

    my thanks… from argentina

  4. Nicolas Says:

    Thanks a lot :)

  5. Tim R. Says:

    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.

  6. Akshay Says:

    Thanks mate, Was very helpful in a moment of need.

    Cheers…

Leave a Comment