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

发布于 2024-12-08 11:47:52 字数 1063 浏览 4 评论 0原文

当尝试使用 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         }

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         }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

迷雾森÷林ヴ 2024-12-15 11:47:52

在文件 /protected/config/main.php 中查找“组件”块并根据需要进行更新:(

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

将 UC 值更改为您的设置)。并阅读这些页面

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.

孤独岁月 2024-12-15 11:47:52

确保您已在正确的配置文件(在 /config 中)中输入数据库信息。据推测,Gii 使用的是 main.php 配置文件,因此请确保您没有在 console.phptest.php 意外地出现在 main.php 上。

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.

橙味迷妹 2024-12-15 11:47:52

如果您确定提供的凭据正确,则 yii 用户不得具有从 localhost 连接的权限。 MySQL 身份验证是通过用户名、密码和主机完成的,因此读起来就像其中之一不正确。

If you are sure that the supplied credentials are correct, then the yii user must not have permission to connect from localhost. MySQL authentication is done by username, password, and host, so it reads like one of those is incorrect.

雪若未夕 2024-12-15 11:47:52

如果您在本地计算机上使用 mamp,请确保在 main.php 文件中将您的用户名设置为“root”,并将密码设置为“root”。默认情况下将密码设置为空,这将导致上述错误。

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. The default is to set password to null and this will cause the error above.

躲猫猫 2024-12-15 11:47:52

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

make sure you change the credentials in console.php ( yiic will not look for main.php , though it has the same thins repeated )

四叶草在未来唯美盛开 2024-12-15 11:47:52

这里我有工作代码:

OS Ubuntu:

<?php

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

为我工作:-)

Here I have working code:

OS 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 :-)

风轻花落早 2024-12-15 11:47:52

我遇到了这个问题。
解决方法:
打开文件 yii/config/db.php
并添加 phpmyadmin 的数据(密码和名称)。

I had this problem.
Solved it by:
Open file yii/config/db.php
and add your data for phpmyadmin (password&name).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文