JDBC Commnunications link failure
This is a post about how to install the JDBC driver for MySQL and how I resolved the error "CommunicationsException: Communications link failure".
To create a Java program that make use of a MySQL database, first you have to install Connector/J — JDBC driver for MySQL.
After download the file mysql-connector-java-5.1.16.tar.gz (5.1.16 is the version as of this writing) from http://www.mysql.com/downloads/connector/j/, extract it and copy the file mysql-connector-java-5.1.16-bin.jar to /usr/lib/java/jre/lib/ext/
You can pick any simple Java app using Google. The following is the full Java error from the commandline (the Java app was compiled successfully):
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:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:344) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2333) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:792) at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 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:582) at java.sql.DriverManager.getConnection(DriverManager.java:207) at de.vogella.mysql.first.MySQLAccess.readDataBase(MySQLAccess.java:22) at de.vogella.mysql.first.test.Main.main(Main.java:8) Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at java.net.Socket.(Socket.java:375) at java.net.Socket.(Socket.java:218) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:294) ... 16 more
There’s load of solutions floating on the Internet and these two provide me a good understanding and also a solution to the issue:
http://stackoverflow.com/questions/2983248/jdbc-with-mysql
https://bbs.archlinux.org/viewtopic.php?pid=721345
Briefly said, the error was due to mysqld was started with the option "--skip-networking", so outside connections to the database were disabled.
On my Slackware box, although this option is commented out in /etc/my.cnf
cat /etc/my.cnf | grep skip-networking #skip-networking
this is also an option in
/etc/rc.d/rc.mysqld. So we have to comment out the following line in that file:
SKIP="--skip-networking"
Now JDBC with MySQL works beautifully.
Of course this is specific to Slackware. On other distributions, I believe there’s a similar way to disable the option "--skip-networking".
Recent Comments