access denied connecting to mysql with yii

16,624

Solution 1

In the file /protected/config/main.php look for the "components" block and update as needed:

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

(change UC vals to your settings). And read these pages.

Solution 2

Make sure you have entered your database info in the correct configuration file (in /config). Presumably Gii is using the main.php config file, so make sure you didn't set up the DB credentials in console.php or test.php by accident instead on main.php.

Share:
16,624
ian
Author by

ian

Updated on June 04, 2022

Comments

  • ian
    ian almost 2 years

    I get this error when trying to connect to my db with Yii

    I am sure the password and permissions are correct. How do I track down the problem here?

    I am running MAMP and the latest 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         }
    
  • ian
    ian over 12 years
    How do I track that down in Yii. I have set up plenty of DB connection in MAMP with no problem.