SQLSTATE[HY000] [1045] 使用 CakePHP 拒绝用户“用户名"@“本地主机"的访问 [英] SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' using CakePHP

查看:59
本文介绍了SQLSTATE[HY000] [1045] 使用 CakePHP 拒绝用户“用户名"@“本地主机"的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to PHP and CakePHP. I am finding problems while wiring my database using CakePHP.

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

Below is my application configuration.

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

I am on Bitnami WAMP stack 5.4.40-0. I am using CakePHP 3.0.4 to create a web MVC application

在我的 app.php 文件中输入数据源.

Entry for datasources in my app.php file.

/**
 * 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 管理员相同的全局权限.

I have already created a database table called jobs according to CakePHP conventions. User test2 has global privileges the same as the root administrator.

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

But when I am running the bake all command, I am getting the following error:

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 的指示.

I followed Ankit and Spencer's directions.

我遇到了一些问题.

  1. 我的用户的主机不是本地主机;它是一个通配符 %.改变了,然后 MySQL 开始拒绝连接.

  1. Host of my user was not localhost; it was a wildcard %. Changed that, then MySQL started refusing connections.

我禁用了我的防火墙,发现端口与 3306 不同.所以我更改了 app.php 中的条目.现在我的应用程序已经出炉了 :)

I disabled my firewall and found that the port was different from 3306. So I changed the entry in app.php. Now my application is baked :)

推荐答案

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

That error message usually means that either the password we are using doesn't match what MySQL thinks the password should be for the user we're connecting as, or a matching MySQL user doesn't exist (hasn't been created).

在 MySQL 中,用户由用户名(test2")主机(localhost")标识.

In MySQL, a user is identified by both a username ("test2") and a host ("localhost").

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

The error message identifies the user ("test2") and the host ("localhost") values...

  'test2'@'localhost'

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

We can check to see if the user exists, using this query from a client we can connect from:

 SELECT user, host FROM mysql.user

我们正在寻找一行,其中 user 为test2",host 为localhost".

We're looking for a row that has "test2" for user, and "localhost" for host.

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

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

If that row doesn't exist, then the host may be set to wildcard value of %, to match any other host that isn't a match.

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

If the row exists, then the password may not match. We can change the password (if we're connected as a user with sufficient privileges, e.g. root

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

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

We can also verify that the user has privileges on objects in the database.

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

<小时>

编辑

如果我们使用 DML 操作(INSERT、UPDATE、DELETE)对 mysql 权限表进行更改,这些更改将在 MySQL 重新读取表之前生效.我们可以通过使用特权用户执行的 FLUSH PRIVILEGES 语句强制重新读取来使更改生效.

If we make changes to mysql privilege tables with DML operations (INSERT,UPDATE,DELETE), those changes will not take effect until MySQL re-reads the tables. We can make changes effective by forcing a re-read with a FLUSH PRIVILEGES statement, executed by a privileged user.

这篇关于SQLSTATE[HY000] [1045] 使用 CakePHP 拒绝用户“用户名"@“本地主机"的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆