CakePHP on OS X can’t connect to MySQL


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.


Related Posts:
  • My Bookmarks
  • Backing up your MySQL data
  • Dept. of Homeland Security and open-source
  • Send Email from Postgresql
  • Aggregating and analyzing logs from multiple web servers

  • Categories: OS X, Software Tags:
    1. Bob Kremmins
      March 26th, 2008 at 05:25 | #1

      Thanks a lot for the help!

      Just what I needed!

    2. May 3rd, 2008 at 20:11 | #2

      excellent, glad i be of some help!

    3. August 13th, 2008 at 15:56 | #3

      hey!!! perfect! works ok!

      sos un capo!

      my thanks… from argentina

    4. Nicolas
      August 13th, 2008 at 16:08 | #4

      Thanks a lot :)

    5. Tim R.
      August 31st, 2008 at 21:47 | #5

      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
      March 29th, 2009 at 23:05 | #6

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

      Cheers…

    1. No trackbacks yet.