简体   繁体   English

CakePHP 2无法连接到MySQL数据库

[英]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. 使用最新的CakePHP 2.0 RC3,我正在尝试连接到MySQL数据库。 For this, I changed the database.php file present in the app/config directory. 为此,我更改了app / config目录中的database.php文件。

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. 对于root,我通过设置密码和使用空白密码来尝试。

  • Tried using the 'root' user as well as by creating another user with the required privileges. 尝试使用“root”用户以及创建具有所需权限的其他用户。
  • Tried giving 127.0.0.1 in place of 'localhost' 尝试用127.0.0.1代替'localhost'
  • Checked that the database was getting connected using normal php script. 检查数据库是否使用普通的PHP脚本连接。

The normal php script to test database connectivity is like:- 用于测试数据库连接的普通php脚本如下: -

<?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. 上面的脚本工作意味着它不是MySQL方面的问题。

Still I always get "Cake is not able to connect to database". 我仍然总是得到“Cake无法连接到数据库”。 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. CakePHP 2.0使用PDO,而不是mysql_connect,我的猜测是没有安装PDO MySQL扩展。

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 !! 我在PDO上搜索一个问题大约一个星期然后我发现我的密码不正确!! 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. 当我启动一个新的CakePHP 2.0应用程序时,我无法连接到MySQL数据库。

I finally figured out that you have to enable the php_pdo_extension in php.ini. 我终于想通了你必须在php.ini中启用php_pdo_extension。

The following link help me to solve this problem 以下链接可以帮助我解决这个问题

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

First test for the PDO Mysql extension via: 通过以下方式首次测试PDO Mysql扩展:

var_dump( extension_loaded('pdo_mysql') );

If it's false, for Windows, just add these lines to your PHP.INI: 如果它是假的,对于Windows,只需将这些行添加到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 参考: 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. 在Windows上,您应该下载最新版本的WAMP,因为CakePHP 2.x使用PDO并且仅支持mySQL 4.最新版本的Cake支持5.x和PHP 5.2.8或更高版本。 Don't forget mod_rewrite if you want it. 如果你想要它,不要忘记mod_rewrite

On Linux you should use apt-get or aptitude : 在Linux上你应该使用apt-getaptitude

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

then restart/reload apache2 然后重启/重新加载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) 最后不要忘记chmod -R 777 cakephp / app / tmp for cache并填写访问你的数据库的领域(app / Config / database.php)

Some CakePHP projects (Such as webzash) have their own database configuration that override the app/Config/Database.php one. 一些CakePHP项目(例如webzash)有自己的数据库配置,可以覆盖app/Config/Database.php For instance, in the case of webzash, the connection is made in plugins/Webzash/Config/MasterConfig.php . 例如,在webzash的情况下,连接是在plugins/Webzash/Config/MasterConfig.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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