Php CDbConnection无法打开数据库连接Yii
当我尝试使用clear PHP连接mysql时,它工作正常。 我的代码 但是当我尝试与yii连接时,却发现了错误 My config/main.phpPhp CDbConnection无法打开数据库连接Yii,php,mysql,yii,Php,Mysql,Yii,当我尝试使用clear PHP连接mysql时,它工作正常。 我的代码 但是当我尝试与yii连接时,却发现了错误 My config/main.php 'db'=>array( 'class'=>'CDbConnection', 'connectionString' => 'mysql:host=hostname;dbname=dbname', 'emulatePrepare' => true, /* I try false too*/
'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);
}
}
}
}
我在展览中看到
$mysqlConnection = new PDO("mysql:host=hostname;dbname= dbname", "username", "password");
我的PHP版本5.5.36 Mysql版本5.5.35
我的主机是i-page.com
Yii版本“1.1.13”
感谢您的帮助。密码的散列方式以及MySQL和MySQL\u PDO库的版本似乎有问题 Yii使用PDO来查询数据库,这就是为什么clear PHP工作得很有魅力,而Yii却没有 要验证这一点,请尝试以下操作:
sudo apt install php-xml php-mbstring php-pdo php-mysql
此行应引发以下错误:
PDO::_构造():服务器请求的身份验证方法客户端未知[mysql_旧密码]
此错误相当于MySQL 2045:
“SQLSTATE[HY000][2054]服务器请求的身份验证方法客户端未知
如果您确认问题与PDO有关,您有几个选项,但您需要访问主机系统(或要求他们解决问题):
- 登录MySQL并执行以下命令
(这将修复pasword的哈希)SET PASSWORD FOR'username'@'hostname'=OLD_PASSWORD('PASSWORD');
- 升级MYSQL PDO库(PDO_MYSQL)以匹配服务器上的MYSQL版本
sudo apachectl restart
安装,然后重新启动apache
sudo service apache2 restart
或:
详细错误是找不到数据库库
我的解决方案与缓存相关:
cache'=>array(
'class'=>'system.caching.CDbCache',
//'class'=>'system.caching.CFileCache',
'connectionID'=>'db',//正在检查,但是'db'数组是否在main/config.php中的'components'数组中?您是否也可以粘贴到如何使用CDbConnection的open函数中?您与pdo相关的问题请致电您的主机进行安装或检查此链接嗨,谢谢您的回复。但是我没有web服务器管理权限,因为我只是使用网络托管,我认为托管管理必须做到这一点。当我解决这个问题时,我会发布解决方案。
sudo apt install php-xml php-mbstring php-pdo php-mysql
sudo apachectl restart
sudo service apache2 restart
cache' => array(
'class' => 'system.caching.CDbCache',
//'class' => 'system.caching.CFileCache',
'connectionID'=>'db', // <<< THIS IS THE ISSUE
),