简体   繁体   中英

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.

  • 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.

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

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.

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/)

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

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

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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM