使用jdbc时拒绝访问数据库

wtlkbnrh  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(276)

我正在开发一个简单的mariadb数据库,名为 movieDB 有一个叫mariadb的用户 customerAgent . 我知道stackoverflow上有许多类似的帐户,但我使用的不是根帐户,而是具有最低权限的普通帐户。
我可以访问数据库 movieDB 在终端中通过ssh,如下所示:

[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]>

但是当我在localhost上执行jdbc代码时,访问在第行被拒绝 stmt.execute("USE movieDB"); :

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

javajdbc代码是:(我已经删除了类中一些不必要的东西,但是如果我遗漏了任何重要的东西,请指出!)

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需要数据库的所有特权,但这听起来既不安全也不安全。我必须拥有所有的特权才能实现我在这里的目标吗?

ev7lccsx

ev7lccsx1#

您的问题是数据库moviddb不存在。不应该是电影吗?

相关问题