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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With