使用JDBC连接到MySQL时访问被拒绝

时间:2017-07-29 22:26:00

标签: java mysql jdbc

这个问题有几次出现,但没有答案提供有关如何修复或诊断问题的信息。 我有一个简单的java应用程序,它使用MySQL JDBC连接到我本地服务器上的MySQL数据库:

    String url = "jdbc:mysql://localhost:3306/dbName";
    String userName = "parser";
    String password = "123";
    try {
        Connection conn = DriverManager.getConnection(url,userName, password);
    } catch (SQLException e1) {
        e1.printStackTrace();
    }

然而,Java会引发异常:

java.sql.SQLException: Access denied for user 'parser'@'localhost' (using password: YES)

simillar问题的许多答案表明用户没有连接权限,但在我的情况下它有:

+---------------------------------------------------------------+
| Grants for parser@%                                           |
+---------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'parser'@'%' WITH GRANT OPTION |
+---------------------------------------------------------------+

任何人都可以向我解释为什么会发生这种情况,如何诊断或了解问题是什么?

更新1: mysql用户的权限列表

+--------------------------+---------------+-------------------------+--------------+
| GRANTEE                  | TABLE_CATALOG | PRIVILEGE_TYPE          | IS_GRANTABLE |
+--------------------------+---------------+-------------------------+--------------+
| 'root'@'localhost'       | def           | SELECT                  | YES          |
| 'root'@'localhost'       | def           | INSERT                  | YES          |
| 'root'@'localhost'       | def           | UPDATE                  | YES          |
| 'root'@'localhost'       | def           | DELETE                  | YES          |
| 'root'@'localhost'       | def           | CREATE                  | YES          |
| 'root'@'localhost'       | def           | DROP                    | YES          |
| 'root'@'localhost'       | def           | RELOAD                  | YES          |
| 'root'@'localhost'       | def           | SHUTDOWN                | YES          |
| 'root'@'localhost'       | def           | PROCESS                 | YES          |
| 'root'@'localhost'       | def           | FILE                    | YES          |
| 'root'@'localhost'       | def           | REFERENCES              | YES          |
| 'root'@'localhost'       | def           | INDEX                   | YES          |
| 'root'@'localhost'       | def           | ALTER                   | YES          |
| 'root'@'localhost'       | def           | SHOW DATABASES          | YES          |
| 'root'@'localhost'       | def           | SUPER                   | YES          |
| 'root'@'localhost'       | def           | CREATE TEMPORARY TABLES | YES          |
| 'root'@'localhost'       | def           | LOCK TABLES             | YES          |
| 'root'@'localhost'       | def           | EXECUTE                 | YES          |
| 'root'@'localhost'       | def           | REPLICATION SLAVE       | YES          |
| 'root'@'localhost'       | def           | REPLICATION CLIENT      | YES          |
| 'root'@'localhost'       | def           | CREATE VIEW             | YES          |
| 'root'@'localhost'       | def           | SHOW VIEW               | YES          |
| 'root'@'localhost'       | def           | CREATE ROUTINE          | YES          |
| 'root'@'localhost'       | def           | ALTER ROUTINE           | YES          |
| 'root'@'localhost'       | def           | CREATE USER             | YES          |
| 'root'@'localhost'       | def           | EVENT                   | YES          |
| 'root'@'localhost'       | def           | TRIGGER                 | YES          |
| 'root'@'localhost'       | def           | CREATE TABLESPACE       | YES          |
| 'mysql.sys'@'localhost'  | def           | USAGE                   | NO           |
| 'parser'@'127.0.0.1'     | def           | SELECT                  | YES          |
| 'parser'@'127.0.0.1'     | def           | INSERT                  | YES          |
| 'parser'@'127.0.0.1'     | def           | UPDATE                  | YES          |
| 'parser'@'127.0.0.1'     | def           | DELETE                  | YES          |
| 'parser'@'127.0.0.1'     | def           | CREATE                  | YES          |
| 'parser'@'127.0.0.1'     | def           | DROP                    | YES          |
| 'parser'@'127.0.0.1'     | def           | RELOAD                  | YES          |
| 'parser'@'127.0.0.1'     | def           | SHUTDOWN                | YES          |
| 'parser'@'127.0.0.1'     | def           | PROCESS                 | YES          |
| 'parser'@'127.0.0.1'     | def           | FILE                    | YES          |
| 'parser'@'127.0.0.1'     | def           | REFERENCES              | YES          |
| 'parser'@'127.0.0.1'     | def           | INDEX                   | YES          |
| 'parser'@'127.0.0.1'     | def           | ALTER                   | YES          |
| 'parser'@'127.0.0.1'     | def           | SHOW DATABASES          | YES          |
| 'parser'@'127.0.0.1'     | def           | SUPER                   | YES          |
| 'parser'@'127.0.0.1'     | def           | CREATE TEMPORARY TABLES | YES          |
| 'parser'@'127.0.0.1'     | def           | LOCK TABLES             | YES          |
| 'parser'@'127.0.0.1'     | def           | EXECUTE                 | YES          |
| 'parser'@'127.0.0.1'     | def           | REPLICATION SLAVE       | YES          |
| 'parser'@'127.0.0.1'     | def           | REPLICATION CLIENT      | YES          |
| 'parser'@'127.0.0.1'     | def           | CREATE VIEW             | YES          |
| 'parser'@'127.0.0.1'     | def           | SHOW VIEW               | YES          |
| 'parser'@'127.0.0.1'     | def           | CREATE ROUTINE          | YES          |
| 'parser'@'127.0.0.1'     | def           | ALTER ROUTINE           | YES          |
| 'parser'@'127.0.0.1'     | def           | CREATE USER             | YES          |
| 'parser'@'127.0.0.1'     | def           | EVENT                   | YES          |
| 'parser'@'127.0.0.1'     | def           | TRIGGER                 | YES          |
| 'parser'@'127.0.0.1'     | def           | CREATE TABLESPACE       | YES          |
| 'simple_cms'@'localhost' | def           | USAGE                   | NO           |
| 'root'@'%'               | def           | SELECT                  | YES          |
| 'root'@'%'               | def           | INSERT                  | YES          |
| 'root'@'%'               | def           | UPDATE                  | YES          |
| 'root'@'%'               | def           | DELETE                  | YES          |
| 'root'@'%'               | def           | CREATE                  | YES          |
| 'root'@'%'               | def           | DROP                    | YES          |
| 'root'@'%'               | def           | RELOAD                  | YES          |
| 'root'@'%'               | def           | SHUTDOWN                | YES          |
| 'root'@'%'               | def           | PROCESS                 | YES          |
| 'root'@'%'               | def           | FILE                    | YES          |
| 'root'@'%'               | def           | REFERENCES              | YES          |
| 'root'@'%'               | def           | INDEX                   | YES          |
| 'root'@'%'               | def           | ALTER                   | YES          |
| 'root'@'%'               | def           | SHOW DATABASES          | YES          |
| 'root'@'%'               | def           | SUPER                   | YES          |
| 'root'@'%'               | def           | CREATE TEMPORARY TABLES | YES          |
| 'root'@'%'               | def           | LOCK TABLES             | YES          |
| 'root'@'%'               | def           | EXECUTE                 | YES          |
| 'root'@'%'               | def           | REPLICATION SLAVE       | YES          |
| 'root'@'%'               | def           | REPLICATION CLIENT      | YES          |
| 'root'@'%'               | def           | CREATE VIEW             | YES          |
| 'root'@'%'               | def           | SHOW VIEW               | YES          |
| 'root'@'%'               | def           | CREATE ROUTINE          | YES          |
| 'root'@'%'               | def           | ALTER ROUTINE           | YES          |
| 'root'@'%'               | def           | CREATE USER             | YES          |
| 'root'@'%'               | def           | EVENT                   | YES          |
| 'root'@'%'               | def           | TRIGGER                 | YES          |
| 'root'@'%'               | def           | CREATE TABLESPACE       | YES          |
| 'parser'@'%'             | def           | SELECT                  | YES          |
| 'parser'@'%'             | def           | INSERT                  | YES          |
| 'parser'@'%'             | def           | UPDATE                  | YES          |
| 'parser'@'%'             | def           | DELETE                  | YES          |
| 'parser'@'%'             | def           | CREATE                  | YES          |
| 'parser'@'%'             | def           | DROP                    | YES          |
| 'parser'@'%'             | def           | RELOAD                  | YES          |
| 'parser'@'%'             | def           | SHUTDOWN                | YES          |
| 'parser'@'%'             | def           | PROCESS                 | YES          |
| 'parser'@'%'             | def           | FILE                    | YES          |
| 'parser'@'%'             | def           | REFERENCES              | YES          |
| 'parser'@'%'             | def           | INDEX                   | YES          |
| 'parser'@'%'             | def           | ALTER                   | YES          |
| 'parser'@'%'             | def           | SHOW DATABASES          | YES          |
| 'parser'@'%'             | def           | SUPER                   | YES          |
| 'parser'@'%'             | def           | CREATE TEMPORARY TABLES | YES          |
| 'parser'@'%'             | def           | LOCK TABLES             | YES          |
| 'parser'@'%'             | def           | EXECUTE                 | YES          |
| 'parser'@'%'             | def           | REPLICATION SLAVE       | YES          |
| 'parser'@'%'             | def           | REPLICATION CLIENT      | YES          |
| 'parser'@'%'             | def           | CREATE VIEW             | YES          |
| 'parser'@'%'             | def           | SHOW VIEW               | YES          |
| 'parser'@'%'             | def           | CREATE ROUTINE          | YES          |
| 'parser'@'%'             | def           | ALTER ROUTINE           | YES          |
| 'parser'@'%'             | def           | CREATE USER             | YES          |
| 'parser'@'%'             | def           | EVENT                   | YES          |
| 'parser'@'%'             | def           | TRIGGER                 | YES          |
| 'parser'@'%'             | def           | CREATE TABLESPACE       | YES          |
+--------------------------+---------------+-------------------------+--------------+

1 个答案:

答案 0 :(得分:0)

似乎是用户"解析器"的许可。是不正确的。您可以使用statement检查user配置。

SELECT * FROM  information_schema.USER_PRIVILEGES; 

如果您已为用户授予新权限,则必须重新加载设置。您可以使用此FLUSH PRIVILEGES;

执行此操作

如何授予不同的用户权限:

  • ALL PRIVILEGES-正如我们之前看到的,这将允许MySQL用户 所有访问指定数据库(如果没有选择数据库, 整个系统)
  • CREATE-允许他们创建新表或 数据库
  • DROP-允许他们删除表或数据库
  • DELETE-允许他们从表INSERT中删除行 - 允许它们 将行插入表格
  • SELECT-允许他们使用Select命令 阅读数据库
  • 更新 - 允许他们更新表格行
  • GRANT OPTION-允许他们授予或删除其他用户'特权

要向特定用户提供权限,您可以使用此框架:

GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;

我希望这可以帮助您解决问题。永远不要忘记冲洗!!!