3

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.

2
  • 1
    Just checking, but is the 'db' array within the 'components' array in your main/config.php? Can you also paste in how you're using the open function of CDbConnection? Commented Apr 2, 2014 at 20:56
  • Your problem related to pdo call your hosting to install it or check this link stackoverflow.com/questions/13375061/… Commented Apr 3, 2014 at 5:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

hi, thanks for reply. But i have no web server admin permission because i just use web hosting, i think that hosting administration must do that. When i solve this problem, i will post the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.