19

I have read about this exception a lot, but I can't solve my.

Mysql server is working.

I can connect to it from NetBeans, but I can't connect to it from java code.

Exception in thread "main" 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:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:243)
    at javaapplication3.JavaApplication3.main(JavaApplication3.java:32)
Caused by: java.net.SocketException: Permission denied: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:241)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
    ... 15 more
Java Result: 1

I catch this exception with any ways to connect to Mysql from java code, but from MysqlWorkBench and from NetBeans is OK. I have no Firewall. I tried 127.0.0.1, result is the same.

Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
user1113159
  • 645
  • 2
  • 7
  • 14
  • 2
    Could you provide your code excample which trow this error? Also, please, specify your operating system, which JDBC file you are using and connection options. – Evgeniy Fitsner Jan 28 '13 at 10:18
  • https://gist.github.com/4654434 – user1113159 Jan 28 '13 at 10:20
  • https://gist.github.com/cf87f9dce231789ea407 – user1113159 Jan 28 '13 at 10:22
  • Windows 7. Standart MysqlDriver(mysql-connector) from NetBeans files. ( I tried to use downloaded from http://dev.mysql.com/downloads/connector/j/ but result is the same. Conection optons are http://piccy.info/view3/4045919/69d57c0c558cb4f6cdbe5de27b15621a/orig/) – user1113159 Jan 28 '13 at 10:30
  • 1
    Caused by: java.net.SocketException: Permission denied: connect It looks like you have no permission to connect, is is not a network problem, maybe username and pw – fredcrs Jan 28 '13 at 10:41
  • This could happen when the server always verifies the certificates provided by users, you can disable SSL and also suppress the SSL errors by adding connection url as jdbc:mysql://localhost:3306/?autoReconnect=true&useSSL=false – Isuru Jul 19 '19 at 07:24
  • Can you please try removing below property spring.datasource.driver-class-name=com.mysql.jdbc.Driver – SUMIT Dec 15 '20 at 05:41

7 Answers7

7

Try to specify the port in

conn = DriverManager.getConnection("jdbc:mysql://localhost/mysql?"
                                        + "user=root&password=onelife");

I think you should have something like this:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?"
                                            + "user=root&password=onelife");

Also, the port number in my example (3306) is the default port, but you may change it while installing MySQL.

I think that a better way to specify password and user is to separate them from the URL like this:

connection = DriverManager.getConnection(url, login, password);
entpnerd
  • 10,049
  • 8
  • 47
  • 68
Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
  • Done. No results. Port is 3306. And port is working. I can successfuly connect from NeatBeans or MysqlWorckBench. – user1113159 Jan 28 '13 at 10:35
  • try this sql-connector http://code.google.com/p/find-ur-pal/downloads/detail?name=mysql-connector-java-5.1.18-bin.jar&can=2&q= – Aliaksei Bulhak Jan 28 '13 at 10:44
  • 2
    I solved it. I'm sorry to bother you all. Problem was in DrWeb firewall. I dont know why, but still I didnt close this programm firewall was working,even if i switched it off. – user1113159 Jan 28 '13 at 10:49
7

Search the file my.cnf and comment the line

skip-networking

to

#skip-networking

Restart mysql

jordanhill123
  • 4,142
  • 2
  • 31
  • 40
Alan Cruz
  • 137
  • 1
  • 3
6

THis issue has been fixed with new mysql connectors, please use http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.38

I used to get this error after updating the connector jar, issue resolved.

lastextinct
  • 61
  • 1
  • 2
5

The problem is mostly due to a MySQL service that is not running, so make sure it is. If it isn't, run this CMD with administrator privilege in order to start it:

sc start [Your MySQL Service name]
Sae1962
  • 1,122
  • 15
  • 31
Marwen Trabelsi
  • 4,167
  • 8
  • 39
  • 80
4

Try the following suggestions:

  1. Your machine may have a static IP; map this IP to the hosts file as localhost.
  2. Try to log in from your computer or within the network via mysql command; if login is successful, it means that MySQL runs fine.
Sae1962
  • 1,122
  • 15
  • 31
M S Parmar
  • 955
  • 8
  • 22
2

It seems that your Java code is using IPv6 instead of IPv4. Please try to use 127.0.0.1 instead of localhost. Ex.: Your connection string should be

jdbc:mysql://127.0.0.1:3306/expeditor?zeroDateTimeBehavior=convertToNull&user=root&password=onelife

P.S.: Please update the URL connection string.

Sae1962
  • 1,122
  • 15
  • 31
Evgeniy Fitsner
  • 946
  • 9
  • 14
2

There are two things

  1. Disable firewall if any or add exception or check if u have correct driver file. Disable any antivirus if any

  2. and also make sure your driver type is mysql.jdbc.driver.

Andri
  • 1,503
  • 13
  • 20
Develop4Life
  • 7,581
  • 8
  • 58
  • 76