简体   繁体   中英

CDbConnection failed to open the DB connection Yii

When i try connect to mysql with clear PHP, its working fine. 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

My 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

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.

It appears to be a problem with the way the passwords are hashed and the version of MySQL and the MYSQL_PDO library.

Yii uses PDO to query the database, thats why clear PHP works like a charm and Yii doesn't.

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].

This error is the equivalent to the MySQL 2045:

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

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

  • Sign in to MySQL and execute the following command 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.

I got this problem with Yii 1.1.x project and almost got crazy:

First make sure you have

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

installed, and restart 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...)

or, you can disable db caching and enable fileCache instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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