Jay
Jay

Reputation: 1478

CakePHP 2 is not able to connect to MySQL database

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.

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.

Upvotes: 8

Views: 34059

Answers (7)

Goose
Goose

Reputation: 4821

Some CakePHP projects (Such as webzash) have their own database configuration that override the app/Config/Database.php one. For instance, in the case of webzash, the connection is made in plugins/Webzash/Config/MasterConfig.php.

Upvotes: 0

user1651354
user1651354

Reputation: 1

I also face this problem. This took me hours to figure out. When I started a new CakePHP 2.0 app I couldn't connect to the MySQL database.

I finally figured out that you have to enable the php_pdo_extension in php.ini.

The following link help me to solve this problem

(http://www.cakephpexample.com/uncategorized/cakephp-missing-database-connection/)

Upvotes: 0

Gladishmare
Gladishmare

Reputation: 343

On Windows you should download the latest version of WAMP because CakePHP 2.x uses PDO and only supports mySQL 4. The latest version of Cake supports 5.x and PHP 5.2.8 or greater. Don't forget mod_rewrite if you want it.

On Linux you should use apt-get or aptitude :

apt-get install apache2 mysql-server php5 ; apt-get install php5-mysql

then restart/reload apache2

Finally don't forget to chmod -R 777 cakephp / app / tmp for cache and fill in the fields of access to your DB (app/Config/database.php)

Upvotes: 0

Breith
Breith

Reputation: 2298

for encoding and error messages :

try {
    $dns = 'mysql:host=localhost;dbname=db';
    $user = 'user';
    $psswrd = 'pass';
    // Options connection
    $options = array(
        PDO::MYSQL_ATTR_INIT_COMMAND    => "SET NAMES utf8",
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    );
    $connection = new PDO( $dns, $user, $psswrd, $options );

} catch ( Exception $e ) {
    echo "Connection impossible to MySQL : ", $e->getMessage();
    die();
}

Good luck

Upvotes: 0

Costa
Costa

Reputation: 4969

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

Upvotes: 1

Bankin
Bankin

Reputation: 797

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.

Upvotes: 1

dhofstet
dhofstet

Reputation: 9964

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();
}

Upvotes: 22

Related Questions