2011-10-07 40 views
2

当我试图连接到我的数据库Yii时出现此错误访问被拒绝连接到mysql与yii

我确信密码和权限是正确的。我如何在这里找到问题?

我正在运行MAMP和最新的Yii。

CDbException 

CDbConnection failed to open the DB connection: SQLSTATE[28000] [1045] Access denied for user 'yii'@'localhost' (using password: YES) 

/Users/user/Dropbox/localhost/yii/framework/gii/generators/model/ModelCode.php(54) 

42  )); 
43  } 
44 
45  public function requiredTemplates() 
46  { 
47   return array(
48    'model.php', 
49  ); 
50  } 
51 
52  public function init() 
53  { 
54   if(Yii::app()->db===null) 
55    throw new CHttpException(500,'An active "db" connection is required to run this generator.'); 
56   $this->tablePrefix=Yii::app()->db->tablePrefix; 
57   parent::init(); 
58  } 
59 
60  public function prepare() 
61  { 
62   if(($pos=strrpos($this->tableName,'.'))!==false) 
63   { 
64    $schema=substr($this->tableName,0,$pos); 
65    $tableName=substr($this->tableName,$pos+1); 
66   } 
+0

您要么使用发出请求的主机发送错误的用户名,要么使用错误的密码。 (MySQL用户/密码对可以是主机特定的)... – ircmaxell

+0

你的db连接信息不正确。检查你的配置关于数据库信息。 – RusAlex

+0

你可以在myapp/protected/config/main.php文件中的db连接配置数组中显示代码吗? Plus显示'mysql>的输出SELECT用户,主机,密码FROM mysql.user;'? –

回答

0

如果您确信所提供的凭证是正确的,那么yii用户必须无权从localhost连接。 MySQL身份验证是通过用户名,密码和主机完成的,所以它读起来就像其中一个不正确。

+0

我如何在Yii中追踪这些情况。我已经在MAMP中建立了大量的数据库连接,没有任何问题。 – ian

4

在文件/protected/config/main.php寻找“部件”块并根据需要进行更新:

'components'=>array(
    'db'=>array(
     'connectionString' => 'mysql:host=HOSTNAME;dbname=DBNAME', 
     'emulatePrepare' => true, 
     'username' => 'MYSQL_USER', 
     'password' => 'MYSQL_PASS', 
    ... 

(UC改变瓦尔斯您的设置)。并阅读these pages

1

确保您在正确的配置文件(/config)中输入了数据库信息。据推测,Gii使用的是main.php配置文件,所以请确保您没有在console.phptest.php中设置数据库凭证,而不是在main.php上。

0

如果您在本地机器上使用mamp,请确保您的用户名设置为'root',并且密码在main.php文件中设置为'root'。默认设置是将密码设置为空,这会导致上面的错误。

0

确保您更改console.php凭据(的yiic不会找main.php,尽管它有重复相同的变薄)

0

在这里,我的工作代码:

操作系统Ubuntu的:

<?php 

return [ 
'class' => 'yii\db\Connection', 
'dsn' => 'mysql:host=192.168.1.11;dbname=yiidemo', 
'username' => 'yiidemo', 
'password' => 'yiidemo!23', 
'charset' => 'utf8', 
]; 

:-)

0

工作对我来说我有这个问题。 解决方案:
打开文件yii/config/db.php
并添加您的数据为phpmyadmin(密码&名称)。