0

I have created a Ubuntu ec2 instance in AWS and installed the MySQL server. I've associated the instance with an elastic IP. I'm able to access the MySQL server with this address http://x.x.xxx.xx/phpmyadmin x.x.xxx.xx is my public elastic IP address. I can access the MySQL database with my MySQL workbench from my local machine. But I cant access it with my local java application.

This is my java connection code

conn = DriverManager.getConnection("jdbc:mysql://ec2-x-x-xxx-xx.ap-south-1.compute.amazonaws.com:3306/userDB",
                      "root", "password");

I'm getting the following error

Connecting to database...
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at mysql.connector.java@8.0.23/com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at mysql.connector.java@8.0.23/com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at mysql.connector.java@8.0.23/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)
    at mysql.connector.java@8.0.23/com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)
    at mysql.connector.java@8.0.23/com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
    at mysql.connector.java@8.0.23/com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at firstJDBCProgram.main(firstJDBCProgram.java:20)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at mysql.connector.java@8.0.23/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at mysql.connector.java@8.0.23/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at mysql.connector.java@8.0.23/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at mysql.connector.java@8.0.23/com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at mysql.connector.java@8.0.23/com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
    at mysql.connector.java@8.0.23/com.mysql.cj.NativeSession.connect(NativeSession.java:144)
    at mysql.connector.java@8.0.23/com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:953)
    at mysql.connector.java@8.0.23/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)
    ... 6 more
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:574)
    at java.base/sun.nio.ch.Net.connect(Net.java:563)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
    at java.base/java.net.Socket.connect(Socket.java:648)
    at mysql.connector.java@8.0.23/com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at mysql.connector.java@8.0.23/com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
    ... 9 more
Anjana
  • 366
  • 5
  • 21
  • so these are the firewall rules on aws? what about those on your pc? workbench might be allowed but not java. – user2023577 Mar 27 '21 at 18:15
  • I haven't done anything on my pc. Can you mention the steps? @user2023577 – Anjana Mar 27 '21 at 18:18
  • @Cayetano, Can you help with this issue – Anjana Mar 27 '21 at 18:48
  • We don't know if you have windows, mac or linux or else. But if you don't setup that stuff, then it's probably ok. It could be just that java is trying too hard on ipv6. Try -Djava.net.preferIPv4Stack=true to java command line. – user2023577 Mar 27 '21 at 22:58
  • Im using windows 10.I tried the command, still getting the same error – Anjana Mar 28 '21 at 10:47
  • Run wireshark, what's the difference between when workbench connects and when java fails to connect? Because if they sent the same thing, the NAT/router/etc behavior would be the same. I would definitely examine deeply the windows firewall inbound rules for any blocking rules that would match the java.exe process. Can you write a test java program that simply open a socket to that host on 3306? If such program won't even connect a socket, it's no wonder if the driver can't either. Otherwise I would consider trying a few different connector/j driver versions, just in case of bugs. – user2023577 Mar 28 '21 at 14:39

0 Answers0