0

I have gone through this question about connection issue with java program and many other but its not solving my problem. In all other questions, connection was not possible due to either db not started or wrong port etc. Here DB server is running, I can see process and also can connect with mysql cli like below

mysql -umysql -p mydb

but then with same username,password,host and port (default in my case localhost:3306), I am not able to connect via java program and getting below error

com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at Test.main(Test.java:24)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:244)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
    ... 15 more
ERRROR Communications link failure

What could be the reason ? I am trying to connect in my java program with below simple java code.

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


public class Test {

public static void main(String[] args) {

    Connection conn = null;
    try {               
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb","mysql","password");
        System.out.println("Successfully got connection");

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("ERRROR "+e.getMessage());
    }

    finally
    {
        if(conn!= null)
            try {
                System.out.println("Closing connection");
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ERRROR "+e.getMessage());
            }
    }



}

}

If firewall could be the culprit, how to check on linux machine if any such thing interrupting request to DB.

UPDATE - I found this command to check my firewall and stop it.

service iptables stop

I saw no firewall rule deployed with below command

iptables -L -n

So there is no firewall interruption but still issue persist.

Community
  • 1
  • 1
ashah
  • 169
  • 2
  • 12
  • Try to set the port, in this way jdbc:mysql://localhost:3306/mydb – Andrea Catania Jan 28 '16 at 12:27
  • @AndreaCatania That is unnecessary, the MySQL JDBC driver defaults to 3306 if no port has been specified. – Mark Rotteveel Jan 28 '16 at 12:38
  • The _"java.net.ConnectException: Connection refused"_ is a clear indication that on a network level, there is no port 3306 available or the server is actively rejecting new connections to that port. Either it isn't accepting new connections, the port is blocked by a firewall, or MySQL isn't listening on that IP address (that is: 127.0.0.1 or ::1), or it is listening on a different port. All these options are covered by the duplicate. – Mark Rotteveel Jan 28 '16 at 12:42
  • As I mentioned, I am able to connect with command line option mysql -hlocalhost -P3306 -umysql -p mydb this is perfectly working and I am able to connect well, when I fire netstat I don't see any process in port 3306. Below command doesn't return anything netstat -a | grep 3306 and netstat -a | grep mysql gives me unix 2 [ ACC ] STREAM LISTENING 3712347 /var/lib/mysql/mysql.sock – ashah Jan 28 '16 at 13:10
  • Given that output, it looks like nothing is running on port 3306. The correct commandline option for mysql is `-P 3306` (note the space!), so likely it is simply using the unix socket, you might want to explicitly specify `--protocol=TCP`. – Mark Rotteveel Jan 29 '16 at 11:58
  • Thanks all for kind response. I finally couldn't identify reason why with mysql cli connection working but not with java class so I just exported DB (with mysqldump) to another mysql server running on another machine and point my class there and now everything is normally working. I am clueless why no port shown in netstat command but still able to connect to mysql with mysql command-line but not with java program. – ashah Feb 01 '16 at 09:15

0 Answers0