Billy Moon
Billy Moon

Reputation: 58521

Cake PHP not building Models

I have created my database, and used cake bake my_project to create my project, and now when I try cake bake all form within my project directory, I get an error relating to Mysql. When I view my project from the browser, it all lights up green, including connecting to the database.

Warning Error: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in [/Users/billy/Documents/projects/cakephp/lib/Cake/Model/Datasource/Database/Mysql.php, line 160]

Any ideas as to what this error is about?


EDIT: Added relevant configuration details (php --info |grep mysql)

I don't really know what to look for in here, but maybe someone does...

$ php --info |grep mysql
Configure Command =>  '/var/tmp/apache_mod_php/apache_mod_php-53.3.1~2/php/configure'  '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-dependency-tracking' '--sysconfdir=/private/etc' '--with-apxs2=/usr/sbin/apxs' '--enable-cli' '--with-config-file-path=/etc' '--with-libxml-dir=/usr' '--with-openssl=/usr' '--with-kerberos=/usr' '--with-zlib=/usr' '--enable-bcmath' '--with-bz2=/usr' '--enable-calendar' '--with-curl=/usr' '--enable-exif' '--enable-ftp' '--with-gd' '--with-jpeg-dir=/BinaryCache/apache_mod_php/apache_mod_php-53.3.1~2/Root/usr/local' '--with-png-dir=/BinaryCache/apache_mod_php/apache_mod_php-53.3.1~2/Root/usr/local' '--enable-gd-native-ttf' '--with-ldap=/usr' '--with-ldap-sasl=/usr' '--enable-mbstring' '--enable-mbregex' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-mysql-sock=/var/mysql/mysql.sock' '--with-iodbc=/usr' '--enable-shmop' '--with-snmp=/usr' '--enable-soap' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--with-xmlrpc' '--with-iconv-dir=/usr' '--with-xsl=/usr' '--enable-zend-multibyte' '--enable-zip' '--with-pcre-regex=/usr'
mysql
Client API version => mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
mysql.allow_local_infile => On => On
mysql.allow_persistent => On => On
mysql.connect_timeout => 60 => 60
mysql.default_host => no value => no value
mysql.default_password => no value => no value
mysql.default_port => no value => no value
mysql.default_socket => /var/mysql/mysql.sock => /var/mysql/mysql.sock
mysql.default_user => no value => no value
mysql.max_links => Unlimited => Unlimited
mysql.max_persistent => Unlimited => Unlimited
mysql.trace_mode => Off => Off
mysqli
Client API library version => mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
mysqli.allow_local_infile => On => On
mysqli.allow_persistent => On => On
mysqli.default_host => no value => no value
mysqli.default_port => 3306 => 3306
mysqli.default_pw => no value => no value
mysqli.default_socket => /var/mysql/mysql.sock => /var/mysql/mysql.sock
mysqli.default_user => no value => no value
mysqli.max_links => Unlimited => Unlimited
mysqli.max_persistent => Unlimited => Unlimited
mysqli.reconnect => Off => Off
mysqlnd
mysqlnd => enabled
Version => mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
PDO drivers => mysql, sqlite, sqlite2
pdo_mysql
Client API version => mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
pdo_mysql.default_socket => /var/mysql/mysql.sock => /var/mysql/mysql.sock

Edit: More info

I tried the suggestion from @0k32, but unfortunately it did not work for me. I soft linked php from my XAMPP folder to /usr/bin/php, and checked it was the right one with which php. I then did a php --info | grep sock and got:

MYSQL_SOCKET => /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
mysql.default_socket => /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock => /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
MYSQLI_SOCKET => /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
mysqli.default_socket => /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock => /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

Which seems to suggest that the correct version of MySQL is being activated. Still I am getting exactly the same error.

Upvotes: 0

Views: 1086

Answers (1)

ok32
ok32

Reputation: 1321

I think this issue could appear because of different configuration files (php.ini) used by apache's php module and command line php. Default settings for mysql connections may differ in those configs. Compare phpinfo() output from apache and php --info from command line.

Update:

In your case it seems you are on MacOS and are using MAMP/MAMP PRO. So am I. And I had the same problem. My solution was very simple. I replaced default php cli binary with a symlink to MAMP's php binary:

$ sudo mv /usr/bin/php /usr/bin/php_default
$ sudo ln -s /Applications/MAMP/bin/php/php5.3.6/bin/php /usr/bin/php

MAMP's php is connecting to MySQL via /Applications/MAMP/tmp/mysql/mysql.sock by default and MacOS's default command line php is trying to use /var/mysql/mysql.sock.

Anyway I think it's a good practice to use the same php with the same config for both web and cli.

update:

A smarter solution would be not to replace default php but just add path to MAMP's php binaries to the PATH. So in your ~/.bash_profile:

export PATH=/Applications/MAMP/bin/php/php5.3.6/bin:$PATH

Correct it if needed. Then reopen terminal or execute this: source ~/.bash_profile. Then you can check which php you are using by executing which php. It should point to MAMP's php.

Upvotes: 2

Related Questions