![](/img/trans.png)
[英]yii CDbException: CDbConnection failed to open the DB connection: could not find driver
[英]CDbConnection failed to open the DB connection Yii
当我尝试使用清晰的PHP连接到mysql时,其工作正常。 我的密码
$link = mysql_connect('hostname', 'username', 'password');
if (!$link) {
die('Could not connect');
}
if(mysql_select_db('dbname')){
echo 'Connected successfully';
}
但是当我尝试与yii连接时,出现错误
我的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',
),
这是我在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 dot com Yii版本'1.1.13'感谢您的帮助。
密码的哈希方式以及MySQL和MYSQL_PDO库的版本似乎存在问题。
Yii使用PDO来查询数据库,这就是为什么清晰的PHP像咒语一样起作用而Yii却不这样的原因。
要验证这一点,请尝试以下操作:
$mysqlConnection = new PDO("mysql:host=hostname;dbname= dbname", "username", "password");
该行应引发以下错误:
PDO :: __ construct():服务器请求客户端[mysql_old_password]未知的身份验证方法。
此错误等效于MySQL 2045:
“ SQLSTATE [HY000] [2054]服务器请求客户端未知的身份验证方法
如果您确认问题与PDO有关,则可以有几种选择,但是您需要访问托管系统(或要求他们解决问题):
SET PASSWORD FOR 'username'@'hostname' = OLD_PASSWORD('password');
(这将修复密码的散列) 我在Yii 1.1.x项目中遇到了这个问题,并且几乎发疯了:
首先确保你有
sudo apt install php-xml php-mbstring php-pdo php-mysql
安装,然后重新启动apache
sudo apachectl restart
要么:
sudo service apache2 restart
详细的错误是找不到数据库库 。
我的解决方案与缓存有关:
cache' => array(
'class' => 'system.caching.CDbCache',
//'class' => 'system.caching.CFileCache',
'connectionID'=>'db', // <<< THIS IS THE ISSUE
),
如果未设置connectionID,则数据库缓存默认为/ protected / data目录中的mysqli数据库,如果系统上未安装mysqli驱动程序,则无法访问(专用服务器,DO Drops,xampp / wamp等常见问题)
或者,您可以禁用数据库缓存并启用fileCache。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.