【问题标题】:Cannot connect to mysql programmatically无法以编程方式连接到 mysql
【发布时间】: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'

标签: java mysql


【解决方案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;

【讨论】:

  • 现在它正在连接,但我想知道我在 rashbeery 上运行 mysql 并尝试使用 mysql 工作台使用远程机器连接,为什么它没有从工作台连接。远程连接需要允许tcp连接吗?
  • 您可能正尝试以 root 身份从工作台连接,因此您需要为 root 用户重新运行该过程或使用工作台中的选项才能使用 ssh 隧道。既然您设法连接并且我的回答可以帮助您识别和解决问题,您能否将我的回答标记为解决方案?谢谢。
【解决方案2】:

您实际上是从本地计算机连接的吗?如果没有,您应该用您的主机描述替换“localhost”。

【讨论】:

  • @danielmetzer 你到底想说什么,你能详细解释一下吗?
【解决方案3】:

根据您的情况,我认为您应该使用

    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;
输出:- +-------------------------------------------------- -----+ |授予 newuser@localhost | +-------------------------------------------------- -----+ |将 *.* 上的所有权限授予 'newuser'@'localhost' | +-------------------------------------------------- -----+

As select user() -- 它将显示当前登录的用户。
SELECT User FROM mysql.user ---它将显示在mysql中创建的所有用户。

public static void main(String[] args) 抛出 ClassNotFoundException, SQLException { 尝试 { Class.forName("com.mysql.jdbc.Driver"); // 连接连接 = DriverManager.getConnection(connectionString); 连接连接 = DriverManager.getConnection("jdbc:mariadb://localhost:3306/iot?autoReconnect=true","newuser","password"); System.out.println("已连接--->" + 连接); } 捕捉(SQLException e){ throw new SQLException("无法创建连接:" + e.getMessage()); } }

它运行没有错误。我在这里没有看到任何问题。看看它。

【讨论】:

  • 刷新特权;不需要,如果您仔细阅读 cmets,他已经尝试过但没有成功。
  • 是的!然后是 IP 问题,因为 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0 必须连接到不同的地址。这不是特权问题。 java.sql.SQLException:无法创建连接意味着它必须连接到其他主机,因为密码和用户名似乎正确。
猜你喜欢
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多