php - CDbConnection 无法打开数据库连接 Yii

标签 php mysql yii

当我尝试使用清晰的 PHP 连接到 mysql 时,它工作正常。 我的代码

$link = mysql_connect('hostname', 'username', 'password'); 
if (!$link) { 
      die('Could not connect'); 
} 
if(mysql_select_db('dbname')){
      echo 'Connected successfully'; 
}

但是当我尝试连接 yii 时,出现错误

我的配置/main.php

'db'=>array(
    'class'=>'CDbConnection',
    'connectionString' => 'mysql:host=hostname;dbname=dbname',
    'emulatePrepare' => true,  /* I try false too*/
    'username' => 'username',
    'password' => 'password',
    'charset' => 'utf8',
),

这是我在 open() 函数 framework/db/CDbConnection.php 中打印的异常输出 异常句柄在这里

protected function open()
{
    if($this->_pdo===null)
    {
        if(empty($this->connectionString))
            throw new CDbException('CDbConnection.connectionString cannot be empty.');
        try
        {
            Yii::trace('Opening DB connection','system.db.CDbConnection');
            $this->_pdo=$this->createPdoInstance();
            $this->initConnection($this->_pdo);
            $this->_active=true;
        }
        catch(PDOException $e)
        {
        echo '<pre>';
        var_dump($e); die;
            if(YII_DEBUG)
            {
                throw new CDbException('CDbConnection failed to open the DB connection: '.
                    $e->getMessage(),(int)$e->getCode(),$e->errorInfo);
            }
            else
            {
                Yii::log($e->getMessage(),CLogger::LEVEL_ERROR,'exception.CDbException');
                throw new CDbException('CDbConnection failed to open the DB connection.',(int)$e->getCode(),$e->errorInfo);
            }
        }
    }
}

异常文本

"SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client"

我在显示中看到了

 CDbException

CDbConnection failed to open the DB connection.

我的 PHP 版本 5.5.36 Mysql 版本 5.5.35 我的主机是 i-page.com Yii 版本 '1.1.13' 感谢您的帮助。

最佳答案

这似乎是密码哈希处理方式以及 MySQL 和 MYSQL_PDO 库的版本问题。

Yii 使用 PDO 来查询数据库,这就是为什么清晰的 PHP 很有魅力而 Yii 却没有。

要验证这一点,试试这个:

$mysqlConnection = new PDO("mysql:host=hostname;dbname= dbname", "username", "password");

这一行应该抛出以下错误:

PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password].

这个错误相当于MySQL 2045:

"SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

如果您确认问题与 PDO 有关,您有多种选择,但您需要访问托管系统(或要求他们解决问题):

  • 登录 MySQL 并执行以下命令 SET PASSWORD FOR 'username'@'hostname' = OLD_PASSWORD('password');(这将修复密码的散列)
  • 升级 MYSQL PDO 库 (PDO_MYSQL) 以匹配服务器上的 MYSQL 版本。

关于php - CDbConnection 无法打开数据库连接 Yii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22815492/

相关文章:

mysql - 使用 Symfony-2.0 表单组件将 HTML 保存到数据库

php - MySQL : select the last action of each user

PHP LinkedIn API 与 Javascript LinkedIN API 集成

php - 将权限存储到多维数组php中

php - MySQL - 子查询 - 你的 SQL 语法错误

php - 安装过程中 yii migrate 出现 Yii 2.0 高级错误

javascript - 如何在 Yii javascript 中使用复选框隐藏或显示文本字段

php - jQuery 相当于 PHP 的 strtr

mysql 在 switch 构造中使用替代名称

有空格的php yii表列名