简体   繁体   English

访问被拒绝使用yii连接到mysql

[英]access denied connecting to mysql with yii

I get this error when trying to connect to my db with Yii 尝试使用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. 我正在运行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         }

In the file /protected/config/main.php look for the "components" block and update as needed: 在文件/protected/config/main.php中,查找“ components”块并根据需要进行更新:

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

(change UC vals to your settings). (将UC val更改为您的设置)。 And read these pages . 并阅读这些页面

Make sure you have entered your database info in the correct configuration file (in /config ). 确保在正确的配置文件( /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 . 大概Gii使用的是main.php配置文件,因此请确保您没有意外地在console.phptest.php设置数据库凭据,而不是在main.php

确保您更改console.php中的凭据(yiic不会寻找main.php,尽管它重复了相同的Thins)

Here I have working code: 这里我有工作代码:

OS Ubuntu: 操作系统Ubuntu:

<?php

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

Working for me :-) 为我工作:-)

I had this problem. 我有这个问题。 Solved it by: 解决方法:
Open file yii/config/db.php 打开文件yii/config/db.php
and add your data for phpmyadmin (password&name). 并为phpmyadmin添加您的数据(密码和名称)。

If you are sure that the supplied credentials are correct, then the yii user must not have permission to connect from localhost . 如果您确定提供的凭据正确,则yii用户必须没有权限从localhost连接。 MySQL authentication is done by username, password, and host, so it reads like one of those is incorrect. MySQL身份验证是通过用户名,密码和主机来完成的,因此它读为其中之一是不正确的。

If you are using mamp on a local machine make sure your username is set to 'root' and the password is set to 'root' in the main.php file. 如果在本地计算机上使用mamp,请确保在main.php文件中将用户名设置为“ root”,将密码设置为“ root”。 The default is to set password to null and this will cause the error above. 默认设置为将密码设置为null,这将导致上述错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM