SQLSTATE [HY000] [1045]使用CakePhp拒绝用户'username'@'localhost'

sau*_*rav 16 php mysql cakephp cakephp-3.0

我是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'],
    ],
Run Code Online (Sandbox Code Playgroud)

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

但是当我运行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()
Run Code Online (Sandbox Code Playgroud)

问题已解决(更新)

我遵循了Ankit和Spencer的指示.

我有几个问题.

  1. 我的用户的主机不是localhost,它是通配符%.改变了,然后mysql开始拒绝连接.

  2. 我禁用了防火墙,发现端口与3306不同.因此更改了app.php中的条目.现在我的申请被烘焙:)

spe*_*593 25

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

在MySQL中,用户由username(test2)和host(localhost)标识.

您获取的错误消息标识user(test2)和host(localhost)值...

  'test2'@'localhost'
Run Code Online (Sandbox Code Playgroud)

检查该用户是否存在,使用您可以连接的客户端的此查询:

 SELECT user, host FROM mysql.user
Run Code Online (Sandbox Code Playgroud)

您正在寻找一个test2针对用户和localhost主机的行.

 user     host       
 -------  -----------
 test2     127.0.0.1  
 test2     ::1        
 test2     localhost  
Run Code Online (Sandbox Code Playgroud)

如果该行不存在,则可以将主机设置为通配符值%,以匹配任何其他不匹配的主机.

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

 SET PASSWORD FOR 'test2'@'localhost' = PASSWORD('mysecretcleartextpassword')
Run Code Online (Sandbox Code Playgroud)

还要验证用户是否拥有数据库中对象的权限.

 GRANT SELECT ON jobs.* TO 'test2'@'localhost' 
Run Code Online (Sandbox Code Playgroud)


Ank*_*ise 6

检查以下事情

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


小智 6

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

.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]
Run Code Online (Sandbox Code Playgroud)

MySQL 管理员:

 SELECT user, host FROM mysql.user
Run Code Online (Sandbox Code Playgroud)

谢谢你,斯宾塞7593

安慰:

php artisan cache:clear
php artisan config:cache
Run Code Online (Sandbox Code Playgroud)

现在它对我有用。

Laracast 的答案