-2

I'm trying to connect to a remote MySQL database, version 5.7.1.

This is my code

import java.sql.*; 

public class mysql {
    public static void main(String args[]){  
        try{  
        Class.forName("com.mysql.jdbc.Driver");  
        Connection con=DriverManager.getConnection(  "jdbc:mysql://server:port/database?useSSL=true","user","password");  
        System.out.print("Connected successfully");
        }catch(Exception e){ e.printStackTrace();}  
        } 

I keep getting this error:

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

The last packet successfully received from the server was 1 276 milliseconds ago.  The last packet sent successfully to the server was 1 251 milliseconds ago.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
    at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:202)
    at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4869)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1656)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1217)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2220)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2015)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:768)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:385)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:323)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at mysql.main(mysql.java:7)
Caused by: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
    at java.base/sun.security.ssl.SSLSocketInputRecord.handleUnknownRecord(SSLSocketInputRecord.java:440)
    at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:175)
    at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:110)
    at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1196)
    at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1105)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:399)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:371)
    at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:187)
    ... 18 more

When I try to connect to my database through MySQL Workbench, it works fine. So I don't know why it doesn't work and I'm also using SSL while connecting with MySQL Workbench. I'm using mysql-connector-java-5.1.49. How do I solve this?

Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • looks like it because of your database server is not reachable (may be not started?). Have a look at this answer: https://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – LeTex Nov 28 '20 at 16:32
  • Does this answer your question? [com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure](https://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai) – LeTex Nov 28 '20 at 16:33
  • @LeTex That is a different problem. The problem here is with the SSL handshake. – Mark Rotteveel Nov 28 '20 at 16:37
  • 1
    When you run your application with `-Djavax.net.debug=ssl:handshake:verbose`, what is the output? See also [Troubleshooting JSSE](https://docs.oracle.com/en/java/javase/11/security/java-secure-socket-extension-jsse-reference-guide.html#GUID-D8F6E432-12F2-47B8-9FD0-CE57A4A4F2E1) – Mark Rotteveel Nov 28 '20 at 16:43
  • i still get the same error – yassin swag Nov 28 '20 at 17:00
  • Adding `-Djavax.net.debug=ssl:handshake:verbose` will print additional debugging information on the SSL handshake to the standard output (or possibly the error output) of the application. This could help identify the problem. – Mark Rotteveel Nov 28 '20 at 17:01
  • @MarkRotteveel this is what i got – yassin swag Nov 28 '20 at 17:18

1 Answers1

0

Database

You must enable global access for the user.

flaxel
  • 4,173
  • 4
  • 17
  • 30
DotBeam
  • 1
  • 1