CakePHP 2 无法连接到 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7696283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:17:04  来源:igfitidea点击:

CakePHP 2 is not able to connect to MySQL database

mysqlcakephp

提问by Jay

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.

使用最新的 CakePHP 2.0 RC3,我试图连接到 MySQL 数据库。为此,我更改了 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.
  • Tried giving 127.0.0.1 in place of 'localhost'
  • Checked that the database was getting connected using normal php script.
  • 尝试使用“root”用户以及创建具有所需权限的另一个用户。
  • 尝试用 127.0.0.1 代替 'localhost'
  • 检查数据库是否已使用普通 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". Currently I am not sure what I am missing here.

我仍然总是得到“蛋糕无法连接到数据库”。目前我不确定我在这里缺少什么。

Any pointers to fix the issue will be helpful.

任何解决问题的指针都会有所帮助。

回答by dhofstet

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

回答by user1651354

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

回答by Costa

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

回答by Bankin

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.

检查您提供的密码!我在 PDO 上搜索了大约一个星期的问题,然后我才发现我的密码不正确!!所以也要注意这一点——错误是一样的。

回答by Breith

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

祝你好运

回答by Gladishmare

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_rewriteif you want it.

在 Windows 上,您应该下载最新版本的 WAMP,因为 CakePHP 2.x 使用 PDO 并且仅支持 mySQL 4。最新版本的 Cake 支持 5.x 和 PHP 5.2.8 或更高版本。mod_rewrite如果你想要,不要忘记。

On Linux you should use apt-getor 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 / tmpfor cache and fill in the fields of access to your DB (app/Config/database.php)

最后不要忘记chmod -R 777 cakephp / app / tmp缓存并填写访问您的数据库的字段(app/Config/database.php)

回答by Goose

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

一些 CakePHP 项目(例如 webzash)有自己的数据库配置来覆盖该配置app/Config/Database.php。例如,在 webzash 的情况下,连接是在plugins/Webzash/Config/MasterConfig.php.