0

I got a google cloud platform - compute engine instance, which I installed MySQL server on.

And now I can't get any signal of life our of the VM the sql installed on, for exsample:

package com.company;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {

public static void connection(){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("in conncection");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public static void connectToMySQL(){
    connection();
    String host = "jdbc:mysql://hotsIP:3306/DBname";
    String user = "user";
    String pass = "password";
    try {
        Connection connection = DriverManager.getConnection(host,user,pass);
        System.out.println("???");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    connectToMySQL();
}
}

It's take a few second like he trying to connect and the EXEPTION

in conncection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

What I done to make it work:

  • in the my.conf:bind address = 0.0.0.0, skip-external-locking comment out
  • restart the server
  • looked if the server is active
  • looked if the server listening to the port
  • looked if its TCP

I don't know what to do anymore.

Aharon
  • 81
  • 8
  • put `Class.forName("com.mysql.jdbc.Driver");` before `Connection connection = DriverManager.getConnection(host,user,pass);` don't use a separated method to load your driver `connection();` another option, try without loading your driver since 2007 you don't need to load your driver, another thing your jdbc connection version is old, try to upgrade it to a new version and check the difference – Youcef LAIDANI Apr 14 '17 at 08:59
  • thanx for commenting, but same EXEPTION – Aharon Apr 14 '17 at 09:12
  • what exception you get explain more? – Youcef LAIDANI Apr 14 '17 at 09:18
  • the intelliJ say "Caused by: java.net.ConnectException: Connection timed out: connect" – Aharon Apr 14 '17 at 09:22
  • did you change the driver or what you change? – Youcef LAIDANI Apr 14 '17 at 09:23
  • i mooved the driver out of the other method "Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(host,user,pass); System.out.println("???");" – Aharon Apr 14 '17 at 09:28
  • ok can you ping your server and check if the server response or not, if you want to check it grammatically you can use this piece of code http://stackoverflow.com/a/40571839/5558072 to check your server response – Youcef LAIDANI Apr 14 '17 at 09:31
  • i ren the code u gave me and its returning "java.net.ConnectException: Connection timed out: connect" - and false in the end – Aharon Apr 14 '17 at 09:43
  • good, then your connection between you and between your server is not enabled, so you have to check your connection first maybe your server is not alive, or you don't have permission to access to this server, another thing, you can open your cmd or terminale and make this command `ping server_ip` and check what it return – Youcef LAIDANI Apr 14 '17 at 09:45
  • i got this when i try to ping "reply from hostIP bytes = 32 time = 79 ms TTL = 56" 4 rows like this then "sent 4 packets , recived 4 + im probbly need to metion that i also installed apatche tomcat witch is working perfectyl + what is premission im connecting with root and i created a new user with all the previliges still nothing – Aharon Apr 14 '17 at 09:57
  • ok with your OS you can ping, but with your program no, what is that mean, check the port number maybe!! – Youcef LAIDANI Apr 14 '17 at 10:07
  • Checked 9 times in deferen methods it's on 3306 the default one – Aharon Apr 14 '17 at 10:33
  • mmm, ok did you changed your jdbc-connector change it to a new version for example you can use maven dependency and you can download the jar if you don't use maven from here https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.6 – Youcef LAIDANI Apr 14 '17 at 10:35
  • same EXCEPTION "Caused by: java.net.ConnectException: Connection timed out: connect" – Aharon Apr 14 '17 at 11:45
  • i discovered now when i scanning with nmap all the ports he didn't say that the port 3306 is opend!!! how can i open this port? – Aharon Apr 14 '17 at 12:06
  • but in my server when look witch port is listening i see tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN – Aharon Apr 14 '17 at 12:32
  • mm, i don't think you need to open the port in your side – Youcef LAIDANI Apr 14 '17 at 12:55
  • So what u suggest me to do next? Plz man I don't know what to do anymore – Aharon Apr 14 '17 at 13:09
  • can you be patient and learn this http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql – Youcef LAIDANI Apr 14 '17 at 13:13
  • sorry but nothing helps me i figured that when i put port 80 the EXCEPTION is changed and its says that "Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost." – Aharon Apr 15 '17 at 22:31
  • Possible duplicate of [com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure](http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai) – zloster Apr 16 '17 at 15:27
  • Been there did that.. if I deploy my spring boot web app on apache tomcat server and that server heve MySQL server install on if I put localhost in the property's file it will work?? – Aharon Apr 17 '17 at 17:34

2 Answers2

0

You have to make the following change to your my.cnf file

my.cnf

bind-address = www.000webhost.com (OR)
bind-address = xx.xx.xx.xx (IP Address)

You need to restart your MySQL service, once this setting is changed.

Also worth noting is the point that MAMP/ MAMP Pro sets MAMP_skip-networking_MAMP by default. You've to disable this line in your my.cnf

And if you don't have any user login issues, you should be able to connect to the MySQL Database from your Java code.

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
0

In my case the root cause was: Firewall. I was trying to run the application at work.

What was interesting is that the App Engine Standard running locally actually generated a non-error log in Google Cloud Platform Logs, making me discard the firewall hypotheses.

Solution: I found out bringing my notebook from home and connecting to company's network, did not work. When I connected to the shared connection in my mobile, worked perfectly.