【发布时间】:2018-06-17 07:35:06
【问题描述】:
我正在开发一个名为 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 需要对数据库具有所有权限,但这听起来既不安全也不安全。是否必须拥有所有特权才能实现我在这里尝试做的事情?
【问题讨论】:
-
你读过异常吗?
movidDB!=movieDB... -
是的,我确实阅读了异常,但我错过了错字是多么愚蠢!!!
-
我的天啊,我花了整个下午在谷歌的几十个搜索和 SO 答案中寻找答案......
-
在使用 JDBC 时,您永远不应该使用像
USE <databasename>这样的命令,您应该通过在 URL 中指定直接连接到 movieDB,或者使用setCatalog(..),另请参见 MySQL Connector/J documentation。使用use <database-name>可能会使连接处于不正确的状态,这可能会导致以后难以识别的错误。 -
@MarkRotteveel 谢谢,马克!非常感谢您的建议!我正在重写连接部分..
标签: java mysql shell jdbc mariadb