简体   繁体   English

使用JDBC时拒绝访问数据库

[英]Access Denied for database when using JDBC

I am working on a simple mariaDB database called movieDB and there is a mariaDB user called customerAgent . 我正在开发一个名为movieDB的简单mariaDB数据库,并且有一个名为customerAgent的mariaDB用户。 I know there are many similar on StackOverflow, but I am not using a root account, but a normal account with minimum granted privileges. 我知道在StackOverflow上有很多类似的东西,但是我没有使用root帐户,而是具有最低授予权限的普通帐户。


I can access the database movieDB in terminal via SSH like this: 我可以像这样通过SSH在终端中访问数据库movieDB

[root@myServer]# mysql -ucustomerAgent -p123

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 102
Server version: 10.2.12-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> USE movieDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [movieDB]> show grants;
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for customerAgent@localhost                                                                                         |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT Customer_Role TO 'customerAgent'@'localhost'                                                                   |
| GRANT USAGE ON *.* TO 'customerAgent'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257'       |
| GRANT USAGE ON *.* TO 'Customer_Role'                                                                                      |
| GRANT SELECT ON `movieDB`.* TO 'Customer_Role'                                                                             |
| GRANT SELECT, INSERT ON `movieDB`.`orders` TO 'Customer_Role'                                                              |
+----------------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

MariaDB [movieDB]> select current_role();
+----------------+
| current_role() |
+----------------+
| Customer_Role  |
+----------------+
1 row in set (0.00 sec)

MariaDB [movieDB]> 

But when I execute JDBC codes on localhost, access is denied at the line stmt.execute("USE movieDB"); 但是,当我在本地主机上执行JDBC代码时,在stmt.execute("USE movieDB");行处拒绝访问stmt.execute("USE movieDB"); :

Access denied for user 'customerAgent'@'localhost' to database 'movidDB'

The java JDBC codes are: (I have removed some unnecessaries in the class, but in the case that I missed anything important, please do point out!) Java JDBC代码为:(我已经删除了该类中的一些不必要的东西,但是如果我错过了任何重要的事情,请务必指出!)

import java.sql.*;
import java.util.*;

Class movieDBFoundation {
    static private String DBServerAddress = "localhost";
    static private Connection conn;

    static private String getDBServerAddress() {
        return DBServerAddress;
    }

    public static void main(String[] args) {

        System.out.println("Connection started.");

        if(DBConnect()) {
            System.out.println("Connection succedded.");
        }
        else {
            System.out.println("Connection failed.");
            return;
        }            
    }

    static private Boolean DBConnect() {
        String connectString = "jdbc:mysql://" + getDBServerAddress() + ":3306/"
              + "?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&&useSSL=false";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(connectString, "customerAgent", "123");
            System.out.println("Connection reached.");
            Statement stmt;
            stmt = conn.createStatement();
            String SQL = "USE movidDB";
            stmt.execute(SQL);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }
}

Some answers in similar questions say JDBC need all privileges on a database, but that does not sound very safe nor secure. 一些类似问题的答案说JDBC需要数据库上的所有特权,但这听起来并不安全。 Is it a must to have all privileges to achieve what I am trying to do here? 必须拥有所有特权才能实现我在这里试图做的事情吗?

Your problem is that the database movidDB don't exist. 您的问题是数据库movidDB不存在。 Should it not be movieDB ? 不应该是movieDB吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JDBC连接到MySQL时访问被拒绝 - Access denied when using JDBC to connect to MySQL 通过浏览器小程序使用JDBC时“访问被拒绝” - “access denied” when using JDBC from a browser applet com.mysql.jdbc.exceptions.jdbc4.CommunicationsException使用JDBC访问远程MySQL数据库时 - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException when using JDBC to access remote MySQL database JDBC访问被拒绝错误 - JDBC Access denied error MySQL访问被拒绝JDBC - Mysql access denied jdbc com.mysql.jdbc.exceptions.jdbc4.MysqlSyntaxErrorException:用户'@'本地主机'对数据库'mysql'的访问被拒绝 - com.mysql.jdbc.exceptions.jdbc4.MysqlSyntaxErrorException:Access denied for user "@'local host' to database 'mysql' com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:用户“ @'localhost”对数据库的访问被拒绝 - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 使用 FileOutputStream 时拒绝访问 - Access is denied when using FileOutputStream 在连接到 mysql 的 jdbc 中拒绝访问 - access denied in jdbc connecting to mysql 使用 JDBC 模板无法访问数据库时出现 SQLNonTransientConnectionException - SQLNonTransientConnectionException when database is unreachable using JDBC template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM