当 mysql 命令行工作时,为什么 PHP PDO 会收到“SQLSTATE[42000] [1044] 用户拒绝访问”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12435471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:33:43  来源:igfitidea点击:

Why does PHP PDO get "SQLSTATE[42000] [1044] Access denied for user" when mysql command line works?

phpmysqldatabasepdodatabase-permissions

提问by Rob Johansen

My head is bloody from how hard I've been banging it against this wall for the past several hours. :(

在过去的几个小时里,我一直在用力撞击这堵墙,我的头都流血了。:(

As the title suggests, I've created a MySQL user that can access the database finefrom the mysql command prompt on the database server. However, when I try to instantiate a new PDO object to access the database with that same user, I get:

正如标题所暗示的,我已经创建了一个可以访问数据库MySQL用户罚款从mysql命令在数据库服务器上的提示。但是,当我尝试实例化一个新的 PDO 对象以使用同一用户访问数据库时,我得到:

SQLSTATE[42000] [1044] Access denied for user 'bob'@'localhost' to database 'my_database'

Here's how I created the user:

这是我创建用户的方式:

GRANT SELECT, DELETE, EXECUTE, INSERT, UPDATE ON my_database.* TO 'bob'@'localhost' IDENTIFIED BY 'some_password';

What could be the problem here?! Please someone throw me a bone! (FYI, the problem happens when I try to create a new PDO object...I catch a PDOException and that's the message).

这里可能有什么问题?!请有人扔给我一根骨头!(仅供参考,当我尝试创建一个新的 PDO 对象时会出现问题......我捕获了一个 PDOException,这就是消息)。

I did FLUSH PRIVILEGES after the grant, and here's the output of SHOW GRANTS:

我在授予后做了 FLUSH PRIVILEGES,这是 SHOW GRANTS 的输出:

mysql> SHOW GRANTS FOR 'bob'@'localhost';
+------------------------------------------------------------------------------------------------------------+
| Grants for bob@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'bob'@'localhost' IDENTIFIED BY PASSWORD '.........................................' |
| GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE ON `my_database`.* TO 'bob'@'localhost'                      |
+------------------------------------------------------------------------------------------------------------+

And here's what mysql.db looks like for this user:

以下是该用户的 mysql.db 文件:

mysql> SELECT * FROM db WHERE User = 'bob'\G;
*************************** 1. row ***************************
                 Host: localhost
                   Db: my_database
                 User: bob
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: N
            Drop_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
Create_tmp_table_priv: N
     Lock_tables_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
         Execute_priv: Y
           Event_priv: N
         Trigger_priv: N

In case it matters, this is a four-node MySQL cluster running on Ubuntu 12.04 LTS.

以防万一,这是一个在 Ubuntu 12.04 LTS 上运行的四节点 MySQL 集群。

EDIT: I've discovered that the problem only occurs when I try to access the server using Zend AMF. Any ideas why PDO wouldn't work with Zend AMF? Have I perhaps missed something in my Zend AMF setup?

编辑:我发现只有在我尝试使用 Zend AMF 访问服务器时才会出现问题。任何想法为什么 PDO 不能与 Zend AMF 一起工作?我是否在 Zend AMF 设置中遗漏了什么?

回答by Synthead

Try 'bob'@'127.0.0.1' instead. If php is accessing it via 127.0.0.1, it'll never be referred to as 'localhost' since the local DNS resolution didn't happen, and MySQL will deny access to it.

试试 'bob'@'127.0.0.1' 代替。如果 php 通过 127.0.0.1 访问它,它永远不会被称为“localhost”,因为本地 DNS 解析没有发生,而且 MySQL 将拒绝访问它。

回答by Ergec

For future Googlers, I had the same problem just now and I was pretty sure that password was correct. Yes password was correct indeed but the problem is how I generate password and how I keep password in config file.

对于未来的 Google 员工,我刚才也遇到了同样的问题,而且我非常确定密码是正确的。是的,密码确实正确,但问题是我如何生成密码以及如何在配置文件中保存密码。

If you use a random password generator like me make sure you don't have $dollar sign in your password.

如果您使用像我这样的随机密码生成器,请确保您$的密码中没有美元符号。

If you have $in you password then make sure you keep you password in config file using single-quotes like this

如果您有$密码,请确保使用这样的单引号将密码保存在配置文件中

$pass = 'randomchars$morerandom';

but not with double-quotes like this

但不是像这样的双引号

$pass = "randomchars$morerandom";