CakePHP Database connection “Mysql” is missing, or could not be created in CakePHP Console SOLVED

Today, I spent some time helping a partner of mine trying to figure out how to enable and use the CakePHP console in Mac OS + XAMPP (Mac OS version of course), so, after searching on the internet what was going on, I realized there was a problem with the unix_socket for MySQL because Mac OS didn’t recognize this parameter automatically, so here is how I solved this issue, I hope this help you a lot.

Cake-logo

Problem

CakePHP Database connection “Mysql” is missing, or could not be created
Error: Database connection "Mysql" is missing, or could not be created.
#0 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Model/Datasource/DboSource.php(260): Mysql->connect()
#1 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Model/ConnectionManager.php(105): DboSource->__construct(Array)
#2 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Console/Command/Task/ModelTask.php(927): ConnectionManager::getDataSource('default')
#3 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Console/Command/Task/ModelTask.php(864): ModelTask->getAllTables('default')
#4 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Console/Command/Task/ModelTask.php(954): ModelTask->listAll('default')
#5 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Console/Command/BakeShell.php(150): ModelTask->getName('default')
#6 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Console/Shell.php(437): BakeShell->all()
#7 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Console/ShellDispatcher.php(207): Shell->runCommand('all', Array)
#8 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Console/ShellDispatcher.php(66): ShellDispatcher->dispatch()
#9 /Applications/XAMPP/xamppfiles/htdocs/projects/site/lib/Cake/Console/cake.php(49): ShellDispatcher::run(Array)
#10 {main}

Solution (in Mac OSX + XAMPP)

Adding the MySQL socket  to YOUR_SITE/app/Config/database.php

'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'

This is how my database.php file looks like:

class DATABASE_CONFIG {

	public $default = array(
		'datasource' => 'Database/Mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'my_user',
		'password' => 'my_password',
		'database' => 'my_database',
		'prefix' => '',
		'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'
		//'encoding' => 'utf8',
	);

	public $test = array(
		'datasource' => 'Database/Mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'user',
		'password' => 'password',
		'database' => 'test_database_name',
		'prefix' => '',
		//'encoding' => 'utf8',
	);
}

 

This is link to stack overflow where I found more details about it: http://stackoverflow.com/questions/19280245/cakephp-database-connection-mysql-is-missing-or-could-not-be-created

That’s it for today, remember: be happy with your code!

Leave a Reply

Your email address will not be published. Required fields are marked *