Reputation: 2664
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.
Upvotes: 3
Views: 24023
Reputation: 499
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.
Upvotes: 2
Reputation: 2107
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):
SET PASSWORD FOR 'username'@'hostname' = OLD_PASSWORD('password');
(this will fix the hashing of the pasword)Upvotes: 1