【发布时间】:2018-09-14 06:11:39
【问题描述】:
我已经在 linux 机器上设置了 mysql (mariadb)。我创建了一个用户“newuser”,例如:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
当我这样做时:
select user,password from user;
我可以看到:
+--------------+-------------------------------------------+
| user | password |
+--------------+-------------------------------------------+
| root | *password |
| newuser | *password |
+--------------+-------------------------------------------+
当我执行这个命令时:
select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
我正在尝试使用 java 连接到我的数据库,但出现以下异常:
java.sql.SQLException: Cannot create connection:Access denied for user 'newuser'@'localhost' (using password: YES)
+------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for newuser@localhost |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost' IDENTIFIED BY PASSWORD '*7ABDF971526E9441B919C9FE77D50DB0363B3509' WITH GRANT OPTION |
+------------------------------------------------------------------------------------------------------------------------------------------------+
连接数据库的Java代码:
try {
Class.forName("com.mysql.jdbc.Driver");
// Connection connection = DriverManager.getConnection(connectionString);
Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/iot?autoReconnect=true","newuser","password");
System.out.println("Connected --->" + connection);
return connection;
} catch (SQLException e) {
throw new SQLException("Cannot create connection:" + e.getMessage());
}
任何人都可以帮我解决我所缺少的吗?
【问题讨论】:
-
你
flush了吗? -
@FedericoklezCulloca 是的,我使用了刷新权限;
-
使用
GRANT不需要刷新 - stackoverflow.com/questions/36463966/… -
@tomaytotomato 是的,我在寻找指向 OP 的文档时发现了这一点。我将把我的评论留在那里,以证明我自己的无知。
-
@Rohitesh 试试这个,看看它说了什么
SHOW GRANTS FOR 'newuser'@'localhost'