If you are going to try out the CakePHP framework from its latest 2.0 channel, that is releases above version 2.0, then when you might face problem in connecting to the database while setting up it even after you have provided correct credentials. If you are lucky then you will even not come across the problem, but for the rest it might get frustrating.

 

cakePHP 2.0 not able to connect to the database

cakePHP 2.0 not able to connect to the database



Cause of the Problem:

The new version of the CakePHP uses the PDO extension instead of mysql or mysqli to connect to the database. So if the extension is not installed along with PHP or it is not enabled to load in the php.ini file, the PDO code statements will not work and hence Cake will not be able to connect to the database.

The Solution:

For windows environment:

Find the following line in the php.ini file and uncomment it (remove the # ) to load the PDO extension at server startup.

Restart your server after saving the file for the change to take effect.

For Linux environment:

Instead of php_pdo_mysql.dll search for pdo_mysql.so line in the php.ini file and uncomment it if it’s commented to load the PDO extension at server startup.

Restart your server after saving the file for the change to take effect.

However, if you see that extension=pdo_mysql.so is enabled and still Cake is unable to connect, you have to specify the unix_socket parameter in the connection array of the DATABASE_CONFIG class present in the database.php file.

The value of unix_socket should be the path to the mysql.sock socket file. The location of the file varies depending upon your lamp stack installation. For me it was present in the above assigned path. You can generally locate it inside the /var/tmp folder or inside the /tmp folder of your mysql installation directory.If you use the unix socket then the connection will be established using that socket, so you can also connect without specifying the host name. Hope it helped.

 

cakePHP 2.0 not able to connect to the database

cakePHP was able to connect after the change