微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLSTATE [HY000] [1045]使用CakePHP对用户'username'@'localhost'的访问被拒绝

如何解决SQLSTATE [HY000] [1045]使用CakePHP对用户'username'@'localhost'的访问被拒绝

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

MysqL中,用户用户名(“ test2”) 主机(“ localhost”)标识。

错误消息标识 用户 (“ test2”)和 主机 (“ 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使用特权用户执行的语句重新读取来使更改生效。

解决方法

我是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的约定。用户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()

解决的问题(更新)

我遵循了安吉特和斯宾塞的指示。

我有几个问题。

  1. 我的用户的主机不是本地主机;那是个通配符%。进行了更改,然后MySQL开始拒绝连接。

  2. 我禁用了防火墙,发现端口不同于3306。因此我更改了中的条目app.php。现在我的应用程序已经烤好了:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。