简体   繁体   English

CDbConnection无法打开数据库连接Yii

[英]CDbConnection failed to open the DB connection Yii

When i try connect to mysql with clear PHP, its working fine. 当我尝试使用清晰的PHP连接到mysql时,其工作正常。 My code 我的密码

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

But when im trying to connect with yii, then getting the error 但是当我尝试与yii连接时,出现错误

My config/main.php 我的config / main.php

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

This is output for exception what i print in open() function framework/db/CDbConnection.php Exception handle here 这是我在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);
            }
        }
    }
}

Exception text 例外文字

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

I see in display 我在显示器上看到

 CDbException

CDbConnection failed to open the DB connection.

My PHP VERSION 5.5.36 Mysql version 5.5.35 My Hosting is i-page dot com Yii Version '1.1.13' Thanks for help. 我的PHP版本5.5.36 Mysql版本5.5.35我的主机是i-page dot com Yii版本'1.1.13'感谢您的帮助。

It appears to be a problem with the way the passwords are hashed and the version of MySQL and the MYSQL_PDO library. 密码的哈希方式以及MySQL和MYSQL_PDO库的版本似乎存在问题。

Yii uses PDO to query the database, thats why clear PHP works like a charm and Yii doesn't. Yii使用PDO来查询数据库,这就是为什么清晰的PHP像咒语一样起作用而Yii却不这样的原因。

To verify this, try this: 要验证这一点,请尝试以下操作:

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

this line should throw the following error: 该行应引发以下错误:

PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password]. PDO :: __ construct():服务器请求客户端[mysql_old_password]未知的身份验证方法。

This error is the equivalent to the MySQL 2045: 此错误等效于MySQL 2045:

"SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client “ SQLSTATE [HY000] [2054]服务器请求客户端未知的身份验证方法

If you confirm that the problem is related to PDO you have several options but you need to access the hosting system (or ask them to fix the problem): 如果您确认问题与PDO有关,则可以有几种选择,但是您需要访问托管系统(或要求他们解决问题):

  • Sign in to MySQL and execute the following command SET PASSWORD FOR 'username'@'hostname' = OLD_PASSWORD('password'); 登录到MySQL并执行以下命令SET PASSWORD FOR 'username'@'hostname' = OLD_PASSWORD('password'); (this will fix the hashing of the pasword) (这将修复密码的散列)
  • Upgrade the MYSQL PDO Library (PDO_MYSQL) to match the version of MYSQL on the server. 升级MYSQL PDO库(PDO_MYSQL)以匹配服务器上的MYSQL版本。

I got this problem with Yii 1.1.x project and almost got crazy: 我在Yii 1.1.x项目中遇到了这个问题,并且几乎发疯了:

First make sure you have 首先确保你有

sudo apt install php-xml php-mbstring php-pdo php-mysql 

installed, and restart apache 安装,然后重新启动apache

sudo apachectl restart

or: 要么:

sudo service apache2 restart

Detailed error was that database library cannot be found . 详细的错误是找不到数据库库

My solution was related to caching: 我的解决方案与缓存有关:

cache' => array(
    'class' => 'system.caching.CDbCache',
    //'class' => 'system.caching.CFileCache',
    'connectionID'=>'db', // <<< THIS IS THE ISSUE
),

if connectionID is not set, db caching defaults to mysqli database in /protected/data directory which cannot be accessed if mysqli driver is not installed on system (common issue with dedicated servers, DO droplets, xampp/wamp...) 如果未设置connectionID,则数据库缓存默认为/ protected / data目录中的mysqli数据库,如果系统上未安装mysqli驱动程序,则无法访问(专用服务器,DO Drops,xampp / wamp等常见问题)

or, you can disable db caching and enable fileCache instead. 或者,您可以禁用数据库缓存并启用fileCache。

暂无
暂无

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

相关问题 yii CDbException:CDbConnection 无法打开数据库连接:找不到驱动程序 - yii CDbException: CDbConnection failed to open the DB connection: could not find driver CDbConnection无法打开数据库连接:在Yii中找不到驱动程序 - CDbConnection failed to open the DB connection: could not find driver in Yii yiic:CDbConnection无法打开数据库连接 - yiic: CDbConnection failed to open the DB connection MySQL:CDbConnection 无法打开数据库连接 - MySQL: CDbConnection failed to open the DB connection Yii 1.1.x CDbHttpSession错误:CDbConnection无法打开数据库连接:找不到驱动程序 - Yii 1.1.x CDbHttpSession error: CDbConnection failed to open the DB connection: could not find driver CDbConnection无法打开数据库连接:在Yii中使用ORACLE数据库时找不到驱动程序 - CDbConnection failed to open the DB connection: could not find driver while using ORACLE database in Yii Yii CDbConnection无法打开数据库连接:找不到google cloud sql的驱动程序 - Yii CDbConnection failed to open the DB connection: could not find driver with google cloud sql CDbConnection无法打开数据库连接:无法打开数据库文件 - CDbConnection failed to open the DB connection: unable to open database file Echofish错误500“ CDbConnection无法打开数据库连接” - Echofish Error 500 “CDbConnection failed to open the DB connection” 测试外部Mysql连接时,我得到CDbConnection未能打开数据库连接 - When testing external Mysql connection, I get the CDbConnection failed to open the DB connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM