SQLSTATE[HY000] [1045] 使用 CakePHP 拒绝用户“username”@“localhost”访问

问题描述 投票:0回答:6

我是 PHP 和 CakePHP 的新手。我在使用 CakePHP 连接数据库时发现问题。

以下是我的应用程序配置。

我使用的是 Bitnami WAMP 堆栈 5.4.40-0。 我正在使用 CakePHP 3.0.4 创建一个 Web MVC 应用程序

我的

app.php
文件中的数据源条目。

/**
 * Connection information used by the ORM to connect
 * to your application's datastores.
 * Drivers include Mysql Postgres Sqlite Sqlserver
 * See vendor\cakephp\cakephp\src\Database\Driver for complete list
 */
'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'nonstandard_port_number',
        'username' => 'test2',
        'password' => 'computer',
        'database' => 'jobs',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,

        /**
         * Set identifier quoting to true if you are using reserved words or
         * special characters in your table or column names. Enabling this
         * setting will result in queries built using the Query Builder having
         * identifiers quoted when creating SQL. It should be noted that this
         * decreases performance because each query needs to be traversed and
         * manipulated before being executed.
         */
        'quoteIdentifiers' => false,

        /**
         * During development, if using MySQL < 5.6, uncommenting the
         * following line could boost the speed at which schema metadata is
         * fetched from the database. It can also be set directly with the
         * mysql configuration directive 'innodb_stats_on_metadata = 0'
         * which is the recommended value in production environments
         */
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
    ],

我已经根据 CakePHP 约定创建了一个名为 jobs 的数据库表。用户test2具有与root管理员相同的全局权限。

但是当我运行bake all命令时,我收到以下错误:

2015-07-01 06:24:56 Error: [PDOException] SQLSTATE[HY000] [1045] Access denied for user 'test2'@'localhost' (using password: YES)
Stack Trace:
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Driver\PDODriverTrait.php(48): PDO->__construct('mysql:host=127....', 'test2', 'computer', Array)
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Driver\Mysql.php(89): Cake\Database\Driver\Mysql->_connect('mysql:host=127....', Array)
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Schema\BaseSchema.php(46): Cake\Database\Driver\Mysql->connect()

问题已解决(更新)

我按照 Ankit 和 Spencer 的指示进行操作。

我遇到了一些问题。

  1. 我的用户的主机不是本地主机;这是一个通配符

    %
    。改变了这一点,然后 MySQL 开始拒绝连接。

  2. 我禁用了防火墙,发现端口与3306不同。所以我更改了

    app.php
    中的条目。现在我的申请已经出炉了:)

php mysql cakephp cakephp-3.0
6个回答
40
投票

该错误消息通常意味着我们使用的密码与 MySQL 认为我们连接的用户的密码不匹配,或者匹配的 MySQL 用户不存在(尚未创建)。

在 MySQL 中,用户由用户名(“test2”)和主机(“localhost”) 来标识。

错误消息标识了 user(“test2”)和 host(“localhost”)值...

  'test2'@'localhost'

我们可以检查用户是否存在,使用来自我们可以连接的客户端的查询:

 SELECT user, host FROM mysql.user

我们正在寻找一行,其中 user 具有“test2”,host 具有“localhost”。

 user     host       
 -------  -----------
 test2     127.0.0.1  cleanup
 test2     ::1        
 test2     localhost  

如果该行不存在,则可以将主机设置为通配符值

%
,以匹配任何其他不匹配的主机。

如果该行存在,则密码可能不匹配。我们可以更改密码(如果我们以具有足够权限的用户身份连接,例如

root

 SET PASSWORD FOR 'test2'@'localhost' = PASSWORD('mysecretcleartextpassword')

我们还可以验证用户是否对数据库中的对象拥有权限。

 GRANT SELECT ON jobs.* TO 'test2'@'localhost' 

编辑

如果我们使用 DML 操作(INSERT、UPDATE、DELETE)对 mysql 权限表进行更改,这些更改将在 MySQL 重新读取表后才会生效。我们可以通过由特权用户执行的

FLUSH PRIVILEGES
语句强制重新读取来使更改生效。


8
投票

检查以下事项

  • 确保 MySQL 服务器正在运行
  • 使用默认凭据检查连接,即用户名:'root'和密码:''[空白密码]
  • 尝试使用相同的凭据登录 phpmyadmin
  • 尝试将 127.0.0.1 替换为 localhost,或者你的 LAN IP 也可以。
  • 确保您在 3306 上运行 MySql,如果您已配置,请确保在建立连接时声明它

5
投票

我看到它已经解决了,但我仍然想分享一个对我有用的解决方案。

.env 文件:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=[your database name]
DB_USERNAME=[your MySQL username]
DB_PASSWORD=[your MySQL password]

MySQL 管理员:

 SELECT user, host FROM mysql.user

Thank you, spencer7593

控制台:

php artisan cache:clear
php artisan config:cache

现在它对我有用。

Laracast 答案


2
投票

如果您使用 MAMP,您可能需要设置套接字: unix_socket:/Applications/MAMP/tmp/mysql/mysql.sock


2
投票

如果所有数据库详细信息正确,请在 .env 文件中使用数据库密码作为字符串。

DB_PASSWORD='your_password'

1
投票

我想补充上面发布的答案,这里提出的解决方案都不适合我。我的 WAMP 正在端口 3308 上工作,而不是默认安装的 3306 端口。 我发现,在本地环境中工作时,如果您在计算机中使用 mysqladmin(用于测试环境),并且使用 3306 以外的端口,则必须使用值 localhost:NumberOfThePort 定义变量 DB_SERVER,因此它将如下所示: 定义(“DB_SERVER”,“localhost:3308”)。您可以通过右键单击任务栏(隐藏图标部分)中的 WAMP 图标并选择“工具”来获取该值。您将看到以下部分:“MySQL 使用的端口:NumberOfThePort”

这将修复您与数据库的连接。

这是我得到的错误: 错误:SQLSTATE[HY1045] 第 X 行上的用户“用户名”@“localhost”的访问被拒绝。

希望这对您有帮助。

:)

© www.soinside.com 2019 - 2024. All rights reserved.