Kgrover
Kgrover

Reputation: 2116

Connecting to MySQL Server JDBC

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.

Upvotes: 1

Views: 11818

Answers (3)

user1439039
user1439039

Reputation: 11

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();
  }
  }
}

Upvotes: 1

Marvo
Marvo

Reputation: 18133

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.

Upvotes: 6

Alex Lynch
Alex Lynch

Reputation: 951

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.

Upvotes: 3

Related Questions