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

在jdbc连接java中设置mysql用户主机,java,mysql,jdbc,Java,Mysql,Jdbc,我正在尝试用java连接到远程mysql服务器,使用JDBC连接器从ip 111.111.111.111连接到ip 111.111.111.222: conn = DriverManager.getConnection("jdbc:mysql://"+serverAddress+":3306/" + this.dBName + "?" + "user=" + this.userName + "&password=" + this.password + ""); user@111.11

我正在尝试用java连接到远程mysql服务器,使用JDBC连接器从ip 111.111.111.111连接到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是在数据库中创建的,授权工作正常

  • user@localhost数据库中不存在,因为我认为不必要
  • 防火墙允许连接
由于某些原因,当我尝试启动连接时,会生成异常:

java.sql.SQLException: The user specified as a definer ('user'@'localhost') does not exist
但是我请求从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)
注意:当我在用户名中写@时,这会显示正确的主机ip,但会有一个额外的@,如下所示:

java.sql.SQLException: Access denied for user 'user@'@'111.111.111.111' (using password: YES)
那么,我做错了什么?或者如何设置mysql用户名的主机

编辑:

此外,我还通过终端测试了mysql连接,它工作正常:(so@SSSip为111.111.111.111)


从我的角度来看,您没有向
用户授予特权

GRANT ALL PRIVILEGES ON *.* TO 'user'@'111.111.111.111' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
执行它并将
密码
替换为您的真实密码

或者,尝试使用
DriverManager.getConnection(字符串url、字符串用户、字符串密码)
而不是

DriverManager.getConnection("jdbc:mysql://"+serverAddress+":3306/" + this.dBName + "?" + "user=" + this.userName + "&password=" + this.password + "");

DriverManager.getConnection(字符串url)
要求url采用
jdbc:subtocol:subname
格式,
getConnection(url、用户名、密码)
更精确

用户
没有连接mysql的权限。您必须授予
用户
权限,“因为我认为不必要。”-检查您的假设。@duffymo,因为我只是远程连接到数据库。那么有必要吗?试试mysql-h111.111.111.111-uuser-p,看看是否可以访问mysqlremotely@HaifengZhang但是我正在用shell测试mysql连接,结果是:
mysql-h 192.xxx.xxx.xxx-u user-p dbName输入密码:。。。欢迎来到MySQL监视器。。。mysql>显示表格;+------------------+|表u in_dbname |+-------------------+…| wh_与_customer |+-------------------+10行在集合中(0,00秒)
因此它确实有效这是以下结果:
显示授权->;+-----------------------------------------------------------+补助金user@111.111.111.%|+----------------------------------------------------------------------------------------+|将**上的用法授予'user'@'111.111.111.%'| |授予选择、插入、更新、删除、,执行,在
dbname
*上显示视图到'user'@'111.111.111.%'|集合中的两行(0,00秒)
并且我刚刚尝试了
conn=DriverManager.getConnection(“jdbc:mysql://“+serverAddress+”:3306/“+this.dbname+”,this.userName+”,this.password)
但它仍然显示
java.sql.SQLException:指定为定义者的用户('user'@'localhost')不存在
,但我不是从localhost而是从不同的ip请求。感谢@HaifengZhangrun
使用GRANT选项将**上的所有权限授予由“密码”标识的“用户”@“本地主机”
刷新权限
将起作用,因为连接将由本地主机进行。无法远程连接并正确设置主机(在这种情况下user@111.111.111.111)? 我无法确定JDBC是否总是以本地主机用户的身份创建连接。
拒绝用户'user@'@'111.111.111'
用户名是什么?是
user
还是
user@
<代码>指定为定义者的用户(“用户”@“本地主机”)不存在
表示
用户
不存在。您使用的用户名是否可能拼写错误?