0

I am trying to connect to MySQL database from Java (MySQL is hosted in WAMP server)

String userName = "root";
String password = "pass";
String url = "jdbc:mysql://localhost/dbase";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password); 

The connection is fine when I am running from localhost. However when I run this code from another computer replacing localhost with my computer's IP (within the same network), I get the error,

message from server: "Host '<name>' is not allowed to connect to this MySQL server"

I have tried with port 3306 too. Whats wrong?

2
  • See here - forums.mysql.com/read.php?52,18966,134534 for most likely source of the problem. Commented Jan 10, 2012 at 1:49
  • I believe mysql defaults only local host connections. You would need to change the config file to allow connections outside of the local host. So add your ip address to the allowed users. Commented Jan 10, 2012 at 1:53

2 Answers 2

5

That's a permission issue on the database side; you need to grant permissions to user root to connect from your specific IP address.

Something like this should work:

GRANT ALL ON foo.* TO root@'1.2.3.4' IDENTIFIED BY 'PASSWORD';

On the other hand; I wouldn't use root for access to the database; you should use a regular user account for this.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your comment. I created another username admin and did Grant All on admin.* To root@'<IP> IDENTIFIED By 'Password' and now get this error. Access denied for user 'admin'@'<computername>' (using password: YES)
@Ankur: Now it looks like you are putting the wrong password? What are you using?
Thanks for your replywell my user:pass was admin:admin and that's what I mentioned in the code.. It isn't wrong.
Fixed it.. Sorry there was already a user named admin in my MySQL. I created another name and it was fine.. Thanks for your help
2

You should not use the root account for development. To solve your problem lets create a hypothetical user called dbasedev.

CREATE USER 'dbasedev'@'%' IDENTIFIED BY 'passw0rd!';
GRANT ALL PRIVILEGES ON *.* TO 'dbasedev'@'%';
FLUSH PRIVILEGES;

This will allow you to connect to the MySQL server with user id: dbasedev, password: passw0rd!, from any host.

This is a complement to @Icarus's answer, I couldn't post all the code in a comment.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.