Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法以编程方式连接到mysql_Java_Mysql - Fatal编程技术网

Java 无法以编程方式连接到mysql

Java 无法以编程方式连接到mysql,java,mysql,Java,Mysql,我已经在linux machiche上安装了mysql mariadb。我创建了一个用户“newuser”,如: CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost'; 当我这样做时: select user,password from user; 我可以看到: +--------------+------------

我已经在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 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
连接到数据库的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());
    } 

有人能帮我做点什么吗?

你真的从本地机器连接吗?如果没有,您应该用您的主机描述替换localhost。

根据您的场景,我认为您应该使用

    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;
作为选择用户-它将显示当前登录的用户。 从mysql.User中选择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()); } }
它正在运行,没有错误。我在这里没有看到任何问题。请查看它。

尝试连接到mysql并执行以下操作

update user set host = '%' where user = 'newuser';
flush privileges;
尝试再次连接。如果您成功了,那么我怀疑您没有在与mysql相同的机器上运行代码。您需要在linux上查找本地计算机的ip 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;

你冲水了吗?@FedericoklezCulloca是的,我使用过冲水特权;使用GRANT不需要刷新-@tomayotatomy-yep,我在查找指向OP的文档时发现了这一点。我将在这里留下我的评论,作为我自己无知的证明。@Rohitesh试试看,看看上面写的是什么“新用户”@“本地主机”@danielmetzer你到底想说什么?你能不能请elobrate?FLUSH特权;是不需要的,如果你仔细阅读评论,他已经尝试了,但没有成功。是的!然后是IP问题,因为10.1.23-MariaDB-9+deb9u1 Raspbian 9.0必须连接到不同的地址。这不是特权问题。java.sql.SQLException:无法创建连接意味着它必须连接到其他主机,因为密码和用户名似乎正确。现在它正在连接,但有一件事我想知道,我正在rashbeery上运行mysql,并试图使用mysql workbench使用远程机器连接,为什么它不从workbench连接。我是否需要允许远程连接的tcp连接?您可能正在尝试以root用户身份从工作台连接,因此需要为root用户重新运行该过程,或者使用工作台中的选项以使用ssh隧道。既然你成功地建立了联系,而我的答案可以帮助你识别和解决问题,你能帮我把我的答案标记为解决方案吗?谢谢
update user set host = 'localhost' where user = 'newuser';
flush privileges;