cake bake MAMP MySQL socket connection refused error

CakePHP MAMP MySQL FAQ: I'm using the "cake bake" command in a CakePHP application I'm developing using MAMP, and I get a MySQL socket connection error, specifically:

Warning: mysql_connect(): [2002] Connection refused (trying to connect via unix:///var/mysql/mysql.sock) 
in /Applications/MAMP/htdocs/myapp/cake/libs/model/datasources/dbo/dbo_mysql.php on line 552

Warning: mysql_connect(): Connection refused in 
/Applications/MAMP/htdocs/myapp/cake/libs/model/datasources/dbo/dbo_mysql.php on line 552

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in 
/Applications/MAMP/htdocs/myapp/cake/libs/model/datasources/dbo/dbo_mysql.php on line 558

Warning: mysql_get_server_info() expects parameter 1 to be resource, boolean given in 
/Applications/MAMP/htdocs/myapp/cake/libs/model/datasources/dbo/dbo_mysql.php on line 566

Warning: mysql_query() expects parameter 2 to be resource, boolean given in 
/Applications/MAMP/htdocs/myapp/cake/libs/model/datasources/dbo/dbo_mysql.php on line 600
Your database does not have any tables.

While this "cake bake" MAMP MySQL socket connection error message looks pretty nasty, it's easily resolved. You just need to make a change in your CakePHP application's database configuration file.

cake bake MAMP MySQL socket connection refused - Problem solved

To fix this "cake bake" MAMP MySQL socket connection error, just edit your CakePHP application's database configuration file:

$ vi app/config/database.php

and change your entry that looks something like this:

var $default = array(
	'driver' => 'mysql',
	'persistent' => false,
	'host' => 'localhost',
	'login' => 'username',
	'password' => 'password',
	'database' => 'databasename',
	'prefix' => '',
);

to this, specifically telling CakePHP which MySQL socket to use, i.e., the MAMP MySQL socket:

var $default = array(
	'driver' => 'mysql',
	'persistent' => false,
	'host' => 'localhost',
	'port' => '/Applications/MAMP/tmp/mysql/mysql.sock',
	'login' => 'username',
	'password' => 'password',
	'database' => 'databasename',
	'prefix' => '',
);

The MySQL port line I added (shown in bold) to this CakePHP database configuration file solves this cake bake MAMP MySQL problem. Once you make that change, your "cake bake" commands should work just fine with your MAMP MySQL database, as it now knows how to find the MAMP MySQL socket.

I hope this CakePHP MySQL socket connection error tip has been helpful.