在jdbc连接java中设置mysql用户主机

时间:2017-02-13 18:18:19

标签: java mysql jdbc

我尝试连接到java中的远程mysql服务器,从ip 111.111.111.111连接到带有JDBC连接器的ip 111.111.111.222:

conn = DriverManager.getConnection("jdbc:mysql://"+serverAddress+":3306/" + this.dBName + "?" + "user=" + this.userName + "&password=" + this.password + "");
  • user@111.111.111.111在数据库中创建,GRANTS正常工作。
  • user @ localhost不存在于数据库中,因为我认为不是必需的。
  • 防火墙允许连接。

出于某种原因,当我尝试启动连接时,会生成异常:

java.sql.SQLException: The user specified as a definer ('user'@'localhost') does not exist

但是我从ip 111.111.111.111

请求连接

注意:当我在用户名中写一个@时,这会显示正确的主机IP,但有一个额外的@,如下所示:

java.sql.SQLException: Access denied for user 'user@'@'111.111.111.111' (using password: YES)

那么,我做错了什么?或者如何设置mysql用户名的主机?

修改

此外,我已经通过终端测试了mysql连接并且它正常工作:(所以@SSS ip是111.111.111.111)

so@SSS:~$ mysql -h 111.111.111.222 -u user -p dbname
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 85
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show tables;
+---------------------+
| Tables_in_dbname |
+---------------------+
| wh_with_customer    |
+---------------------+
10 rows in set (0,00 sec)

1 个答案:

答案 0 :(得分:1)

您从我的角度

未向user授予权限
GRANT ALL PRIVILEGES ON *.* TO 'user'@'111.111.111.111' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

执行它并用您的真实密码替换PASSWORD

或者,尝试使用

以外的DriverManager.getConnection(String url, String user, String password)
DriverManager.getConnection("jdbc:mysql://"+serverAddress+":3306/" + this.dBName + "?" + "user=" + this.userName + "&password=" + this.password + "");

DriverManager.getConnection(String url)期望jdbc:subprotocol:subname格式的网址,getConnection(url, username, password)更精确