Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2 is not able to connect to MySQL database

Tags:

mysql

cakephp

Using the latest CakePHP 2.0 RC3, I am trying to connect to MySQL database. For this, I changed the database.php file present in the app/config directory.

The file contains the below details required for connecting to the database.

class DATABASE_CONFIG {

       public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'root',
        'password' => '',
        'database' => 'db_world',
        'prefix' => ''
       );

}

For root, I tried both by setting the password as well as using a blank password.

  • Tried using the 'root' user as well as by creating another user with the required privileges.
  • Tried giving 127.0.0.1 in place of 'localhost'
  • Checked that the database was getting connected using normal php script.

The normal php script to test database connectivity is like:-

<?php

   $connect = mysql_connect("127.0.0.1","root","") or die("Could not connect");
   mysql_select_db("db_world") or die("Could not find db");

   echo "hello world";

?>

The above script works which means that it is not an issue from MySQL side.

Still I always get "Cake is not able to connect to database". Currently I am not sure what I am missing here.

Any pointers to fix the issue will be helpful.

like image 670
Jay Avatar asked Oct 08 '11 10:10

Jay


3 Answers

CakePHP 2.0 uses PDO, not mysql_connect, and my guess is that the PDO MySQL extension is not installed.

Can you run the following script to check whether you can manually create a connection?

$hostname = "localhost";
$username = "root";
$password = "";

try {
  $db = new PDO("mysql:host=$hostname;dbname=db_world", $username, $password);
  echo "Connected to database";
}
catch(PDOException $e) {
  echo $e->getMessage();
}
like image 155
dhofstet Avatar answered Nov 19 '22 12:11

dhofstet


Check the password you gave ! I was searching for a problem at the PDO about a week then I just found that my password is incorrect !! So pay attention to that also - the error will be the same.

like image 38
Bankin Avatar answered Nov 19 '22 13:11

Bankin


First test for the PDO Mysql extension via:

var_dump( extension_loaded('pdo_mysql') );

If it's false, for Windows, just add these lines to your PHP.INI:

extension=php_pdo.dll   /* not necessary for PHP v5.3+ */
extension=php_pdo_mysql.dll

Reference: http://www.php.net/manual/en/pdo.installation.php

like image 1
Costa Avatar answered Nov 19 '22 12:11

Costa