0

I can't connect to a mysql database on my "live-server" but it works just fine on my local computer.

In my main class I am doing this:

try {
    main.connection.open();
} catch (SQLException e1) {
    log.fatal(e1.getMessage());
    System.exit(0); 
}

And the open method looks like this

public void open() throws SQLException{
    if(con != null) close();
    con = DriverManager.getConnection(url, user, password);
}

After a couple of minutes running I am getting this:

14:35:47.434 [main] FATAL se.mypack.Server - Could not create connection to database server. Attempted reconnect 3 times. Giving up.

How do I debug this? The url, server and password variables is correct. What might be the problem here?

Using mysql-connector-java-5.1.29-bin.jar

daker
  • 3,430
  • 3
  • 41
  • 55
  • check your credentials again. You should be 100% that it they are right. Also see if the user has permissions to access this database - make sure you use the `GRANT` command to give permissions to this user. – Arvind Sridharan Feb 08 '14 at 14:22
  • That has been done. Connecting with the same credentials in php works. And the user also has permissions for localhost which is the server I am using. Do I need to set permissions for the servers local ip aswell? eg. 192.168.0.4 – daker Feb 08 '14 at 14:29
  • yes you would need to do that on the live database if you are trying to connect from your machine. you would need to give the permissions to the external ip of your machine (you can see that if you ssh into it - if it is a linux environment that is) - see this : http://stackoverflow.com/a/8348560/728610 – Arvind Sridharan Feb 08 '14 at 14:34
  • Ok. Stil the same problem though. But wouldnt I get "Access denied for user ..." if there was a privileges thing? Any other ideas? I'm tearing my hair out over this. – daker Feb 08 '14 at 14:40
  • 1
    Log the full stack trace, not just the main message. That will give you more information about why it's failing. Assuming log4j or logback, this would look like: `log.fatal(e1.getMessage(), e1);` – lreeder Feb 08 '14 at 14:48
  • did you try connecting over a simple client like mysql workbench to see what error it gives? – Arvind Sridharan Feb 08 '14 at 14:48
  • Also, if possible, post your JDBC URL (obscure your server, username, and password if needed for security) – lreeder Feb 08 '14 at 14:51
  • Take a look in the MySQL logs and see if that helps, in addition to supply the full stack trace. – mikemil Feb 09 '14 at 03:21
  • can you show me the complete code, for better understanding..please – Jaffer Wilson Jun 12 '15 at 09:20

1 Answers1

-1

First you have to make sure you have loaded your DB-driver class. It goes as the following: Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

This is for DERBY DB. There is a different driver class for each DB, but the idea is the same.

If you already did it, check if you have a tcp connection to the DB server from the client machine that tries to establish the connection. You can use : ping -t your.server.ip. If you get a response, try to establish telet connection to that url + port number (the port that the DB listener listens to, for Oracle it will be 1521, for Derby it is 1527..) and see that you don't get any connection refuse messages. If your connection attempt failed, see if the DB listener is up, or whether you have a fire wall issue.

Good Luck, Yosi Lev

ylev
  • 2,313
  • 1
  • 23
  • 16