CakePHP 2 Cannot Connect to MySQL Database

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

The file contains the following data required to connect 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 to set a password and use an empty password.

  • I tried to use the "root" user, and also created another user with the necessary privileges.
  • Tried to give 127.0.0.1 instead of 'localhost'
  • Checked that the database is connected using a regular php script.

The normal php script for checking the database connection is as follows: -

 <?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 this is not a problem on the part of MySQL.

However, I always get: "Cake cannot connect to the database." Currently, I'm not sure what I am missing here.

Any pointers to fix the problem will be helpful.

+8
mysql cakephp
source share
7 answers

CakePHP 2.0 uses PDO, not mysql_connect, and I assume the MySQL PDO extension is not installed.

Can you run the following script to check if 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(); } 
+22
source share

The first test to extend the Mysql PDO with:

 var_dump( extension_loaded('pdo_mysql') ); 

If this is not the case, 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 

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

+1
source share

Check the password you provided! I searched for a problem in PDO after about a week, and then found that my password is incorrect! Therefore, pay attention to this - the error will be the same.

+1
source share

I also run into this problem. It took me hours to understand. When I started the new CakePHP 2.0 application, I was unable to connect to the MySQL database.

I finally realized that you need to include php_pdo_extension in php.ini.

The following link will help me solve this problem.

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

+1
source share

for coding 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.

0
source share

On Windows, you must 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 higher. Do not 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 reload / reload apache2

Finally, do not forget chmod -R 777 cakephp / app / tmp for the cache and fill in the access fields for your database (app / Config / database.php)

0
source share

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

0
source share

All Articles