簡體   English   中英

使用JDBC時拒絕訪問數據庫

[英]Access Denied for database when using JDBC

我正在開發一個名為movieDB的簡單mariaDB數據庫,並且有一個名為customerAgent的mariaDB用戶。 我知道在StackOverflow上有很多類似的東西,但是我沒有使用root帳戶,而是具有最低授予權限的普通帳戶。


我可以像這樣通過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]> 

但是,當我在本地主機上執行JDBC代碼時,在stmt.execute("USE movieDB");行處拒絕訪問stmt.execute("USE movieDB");

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

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;
    }
}

一些類似問題的答案說JDBC需要數據庫上的所有特權,但這聽起來並不安全。 必須擁有所有特權才能實現我在這里試圖做的事情嗎?

您的問題是數據庫movidDB不存在。 不應該是movieDB嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM