gpt4 book ai didi

php - 无法使用 Yii 框架连接数据库

转载 作者:行者123 更新时间:2023-11-29 15:54:56 25 4
gpt4 key购买 nike

我尝试在远程服务器中运行应用程序,然后通过计算机的浏览器连接此 Web 应用程序。连接数据库时出错。

错误消息是:CDbConnection failed to open the DB connection: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) .

我看到了很多解决方案并尝试过,但没有任何帮助。

当前操作系统是Ubuntu 18.04,nginx版本是nginx/1.14.0 (Ubuntu) ,php版本是5.6.40 ,mysql版本是5.7.24和 yii 1.1.19使用的版本。所有这些堆栈都安装在远程服务器上。

到目前为止我已经尝试过的;我已经检查过该用户是否存在。 SELECT user, host FROM mysql.user;

    | user              | host          |
+-------------------+---------------+
| root | 127.0.0.1 |
| root | localhost |
+-------------------+---------------+

没问题。

我检查了mysql运行端口。

 mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+

我已检查授予权限;

mysql> SHOW GRANTS  For root@localhost;
+----------------------------------------------------------------+
| Grants for root@localhost
|
+----------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
| GRANT SELECT ON `root`.* TO 'root'@'localhost'
|GRANT ALL PRIVILEGES ON `xxxx`.* TO 'root'@'localhost'
| GRANT SELECT ON `xxxx`.* TO 'root'@'localhost'
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+------------------------------------------------------------------+


mysql> SHOW GRANTS FOR CURRENT_USER();
+-----------------------------------------------------------------+
| Grants for root@127.0.0.1 |
+-----------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION
|
| GRANT ALL PRIVILEGES ON `xxxx`.* TO
'root'@'127.0.0.1' |
+----------------------------------------------------------------------+

在php端,有一些由环境分隔的文件。例如:development.php、test.php。

我已经触发运行development.php,它包含一个连接字符串:

 db' => array(
'connectionString' => 'mysql:host=127.0.0.1;dbname=xxxx',
'enableParamLogging' => $profiling,
'enableProfiling' => $profiling,
'schemaCachingDuration' => 0,
),

由另一个 php 文件加载的用户名和密码,如下所示:

 'db' => array(
'class' => 'DbConnection',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'xxxx',
'charset' => 'utf8',
'schemaCachingDuration' => 7200,

),

一切看起来都很正常。我还可以为该用户和密码连接 mysql 终端,如下所示: mysql -u root -p -h 127.0.0.1

但是当我在浏览器中输入网址时,它给出: CDbConnection failed to open the DB connection: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)错误,我找不到解决方法。

最佳答案

也许尝试使用 localhost 而不是 127.0.0.1

return array(
'class' => 'CDbConnection',
'connectionString' => "mysql:host=localhost;dbname=xxxxx;port=3306",
'emulatePrepare' => true,
'username' => 'root',
'password' => 'xxxx',
'charset' => 'utf8',
'enableProfiling' => true,
'enableParamLogging' => true
);

关于php - 无法使用 Yii 框架连接数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56545431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com