1

I am trying to connect to a MySQL Server using JDBC tool in java (using eclipse). I was just wondering how to enter 2 user/password combinations. The first one is the one I use to connect to the server(for example when I ssh into the server) and the second one I enter into phpmyadmin. As of now, I am putting in the phpmyadmin password only into the jdbc connection properties and it's not connecting. This is my current statement:

conn = DriverManager.getConnection("jdbc:mysql://[IP of server]:3306/[Database Table name]", "[UserName (same as phpmyadmin)]","[Password (same as phpmyadmin)]");

I am getting a

java.sql.SQLException: null,  message from server: "Host '[My computer's full host name]' is not allowed to connect to this MySQL server"

I was just wondering if I needed to enter my server login/password (the one I use for ssh) as well in addition to the phpmyadmin username/pwd. I am new to JDBC and MySQL server, so I would appreciate any tips.

Just for background, I am able to connect successfully through ssh and I can login to the server through phpmyadmin.

6
  • Is your MySQL database server running on the same machine as your clients? Commented Jun 18, 2012 at 4:35
  • And no, your server login/password doesn't come into play here. MySQL does its own authentication and authorization. Commented Jun 18, 2012 at 4:36
  • It's not running on the same machine that is trying to connect to it, no. Commented Jun 18, 2012 at 4:38
  • Okay. Is this "Host not allowed..." a common problem, and do you have any idea how to resolve it? Commented Jun 18, 2012 at 4:39
  • 1
    And it's not really a "problem." It's part of MySQL's security model. It just takes some understanding. Commented Jun 18, 2012 at 4:49

3 Answers 3

6

Here's how you can create an account that can access your server from another client machine:

CREATE USER 'bobby'@'localhost' IDENTIFIED BY 'some_password';

That creates the user, and says he can connect from localhost.

If he is on the machine 192.168.0.5, you'd do something like this:

CREATE USER 'bobby'@'192.168.0.5' IDENTIFIED BY 'some_password';

Then of course, you have to grant privileges appropriately:

GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'@'192.168.0.5' WITH GRANT OPTION;

That's been my experience anyway.

You're probably better off reading this section on how to specify MySQL accounts.

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

2 Comments

I cannot try this tonight, but I will tomorrow morning and let you know.
It takes a few tries (or did for me.) The single quotes are important and a bit non-intuitive. And you may have to implement Alex's answer below as well.
3

When you log in from PHPMyAdmin, the web server is located on the same server that hosts the Mysql database (in your case). Mysql, by default, does not allow connections from remote hosts; only the local machine may access the database.

I'll take a shot in the dark and say that the computer you're running the java code on is not the same machine that is hosting the mysql server. You must configure Mysql to allow connections from remote hosts via the config file and then change the Host row of the mysql.users table for the specified user to allow connection from your IP address (or, if security isn't your concern, any IP address.)

To configure mysql to allow connections from remote hosts you must remove the "bind-address=" line from the configuration file.

To allow any host to log on to a specific mysql user, you must set the mysql.users Host` column to "%".

Both of these must be done.

5 Comments

Thank You. I will try it as soon as I can and I'll let you know (and then I'll accept the answer as well).
that is correct. check out cyberciti.biz/tips/…. It doesn't specifically say to remove the bind-address= line, but that's what I did when I had this problem and it worked fine.
@Kgrover Have you solved your problem? If you're having trouble, I can probably help out.
I was trying to do this through android actually, and I ended up using a web script intermediate after research. However, through plain java your answer solved the problem.
I am using 000webhost.com for hosting a database, but I am not able to find any my.ini or my.cnf file.
1
public class MysqlConnect{
  public static void main(String[] args) {
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "jdbctutorial";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"; 
  String password = "root";
  try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  conn.close();
  System.out.println("Disconnected from database");
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.