【问题标题】:Db Error while connecting with Yii Db settings连接 Yii Db 设置时出现 Db 错误
【发布时间】:2014-02-15 14:47:06
【问题描述】:

我收到以下错误。

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

这是我的 Db 设置,现在我是 Yii 的新手,所以我不知道是否还有其他需要提及的内容,所以如果有其他内容请告诉我,我会尽快粘贴。

// 这是 yiic 控制台应用程序的配置。 // 任何可写的 CConsoleApplication 属性都可以在这里配置。

return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'My Console Application',

    // preloading 'log' component
    'preload'=>array('log'),

    // application components
    'components'=>array(
//      'db'=>array(
//          'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
//      ),
        // uncomment the following to use a MySQL database

        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=mydb',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ),

        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
            ),
        ),
    ),
);

【问题讨论】:

  • 你的本地主机上安装了 MySQL 吗?它在运行吗? (您发布的文件是在 控制台/命令提示符 中运行 Yii 的配置。Yii Web 应用程序的配置通常在 main.php 中。)

标签: php yii


【解决方案1】:

我认为你错过了课程:

'db'=>array(
            'class'=>'CDbConnection', // specify class 
            'connectionString' => 'mysql:host=localhost;dbname=mydb',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ),

【讨论】:

  • 还有你的代码放在哪里?我相信它在 protected/config/main.php
  • 啊,我想我发现丢失的文件一直在访问一些远程数据库,因为我正在寻找其他一些名为 config.php 的文件
【解决方案2】:

我认为 mysql 服务器没有在你的机器或你使用的服务器上运行。

【讨论】:

  • 嗯,如何测试我是否在运行远程服务器?
  • 我在本地通过 Xammpp 连接,其他应用程序运行良好,但这不是
  • 我好像没有在你的localhost上启动mysql。我不使用 XAMPP,但我知道它有一个控制面板,您可以在其中查看所有正在运行或未运行的进程。
猜你喜欢
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 2012-01-24
相关资源
最近更新 更多