我在linux machiche上设置了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 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
连接到db的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());
}
任何人都可以帮助我,我错过了吗?
答案 0 :(得分:1)
尝试连接到mysql并执行
update user set host = '%' where user = 'newuser';
flush privileges;
尝试再次连接。如果你成功了(我怀疑)你没有在与mysql相同的机器上运行代码。你需要找到本地机器的ip(linux上的ifconfig,windows上的ipconfig)并执行
update user set host = 'xxx.xxx.xxx.xxx.' where user = 'newuser';
flush privileges;
仅允许来自ip(xxx.xxx.xxx.xxx)的连接。
要还原更改,请执行
update user set host = 'localhost' where user = 'newuser';
flush privileges;
答案 1 :(得分:0)
你真的从本地机器连接?如果没有,您应该将“localhost”替换为您的主机的描述。
答案 2 :(得分:0)
根据您的情况,我认为您应该使用
FLUSH PRIVILEGES;
如果修改了权限,则不需要FLUSH,因为它将被重新加载,但在这里您要创建新用户并向新用户授予新权限,因此请尝试使用FLUSH PRIVILEGES。
我已经使用了以下步骤,它在这里工作正常: -
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
现在使用新用户登录: -
mysql -u newuser -p
password
show GRANTS;
Output:- +------------------------------------------------------+ | Grants for newuser@localhost | +------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost' | +------------------------------------------------------+
作为select user() - 它将显示已登录的当前用户 SELECT User FROM mysql.user ---它将显示在mysql中创建的所有用户。
public static void main(String[] args) throws ClassNotFoundException, SQLException { 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); } catch (SQLException e) { throw new SQLException("Cannot create connection:" + e.getMessage()); } }
它运行没有错误。我在这里看不到任何问题。看看它。