我正在开发一个名为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]>
但是当我在localhost上执行JDBC代码时,在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需要数据库上的所有特权,但这听起来不安全也不安全。是否必须拥有所有特权才能实现我在这里要做的事情?
答案 0 :(得分:3)
您的问题是数据库 movidDB 不存在。它应该不是 movieDB 吗?