Sergio Baquero
Sergio Baquero

Reputation: 41

Set mysql user host in jdbc connection java

I'm trying to connect to remote mysql server in java, from ip 111.111.111.111 to ip 111.111.111.222 with JDBC connector:

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

For some reason when I try to start the connection an exception is generated:

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

but I'm requesting the connection from ip 111.111.111.111

Note: when I write an @ in the username this shows the correct host ip but with an extra @, like this:

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

So, What am I doing wrong? or how can set the host of the mysql username?

EDIT:

Additionally I've tested by terminal the mysql connection and it works properly: (so@SSS ip is 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)

Upvotes: 0

Views: 1152

Answers (1)

Haifeng Zhang
Haifeng Zhang

Reputation: 31925

You didn't grant privileges to user from my perspective

GRANT ALL PRIVILEGES ON *.* TO 'user'@'111.111.111.111' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

Execute it and replace PASSWORD with your real password.

Alternatively, try using DriverManager.getConnection(String url, String user, String password) other than

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

DriverManager.getConnection(String url) expects URL in jdbc:subprotocol:subname format, getConnection(url, username, password) is more precise

Upvotes: 1

Related Questions