20

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.

7
  • 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. Commented Jan 28, 2013 at 10:18
  • gist.github.com/4654434 Commented Jan 28, 2013 at 10:20
  • gist.github.com/cf87f9dce231789ea407 Commented Jan 28, 2013 at 10:22
  • Windows 7. Standart MysqlDriver(mysql-connector) from NetBeans files. ( I tried to use downloaded from dev.mysql.com/downloads/connector/j but result is the same. Conection optons are piccy.info/view3/4045919/69d57c0c558cb4f6cdbe5de27b15621a/orig) Commented Jan 28, 2013 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 Commented Jan 28, 2013 at 10:41

7 Answers 7

7

Search the file my.cnf and comment the line

skip-networking 

to

#skip-networking 

Restart mysql

Sign up to request clarification or add additional context in comments.

1 Comment

In newer versions you have to comment out #bind-address = 127.0.0.1
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); 

3 Comments

Done. No results. Port is 3306. And port is working. I can successfuly connect from NeatBeans or MysqlWorckBench.
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.
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.

Comments

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] 

Comments

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.

Comments

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.

2 Comments

Disabling windows firewall worked for me.
Does it work if the driver type is com.mysql.jdbc.Driver (capital letter 'D')?
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.

3 Comments

read post accurately. he wrote that 'I tried 127.0.0.1, result is the same'
I did it. Result is the same. :-(
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.