↓ Skip to main content

Connection refused from MySQL

·221 words·2 mins
Sergii Zolotarov
Author
Sergii Zolotarov
Twenty years of Java, from Kyiv to Leeuwarden. Currently at Booking.com

This morning I had to setup one MySQL database on my home computer (Zenwalk Linux 5.0), everything went smoothly until I needed to connect to the database from Java using the mysql-connector. What surprised my, MySQL Admin front end connected to the database ok, and with the JDBC I see the following error:


An error occured while establishing the connection.
   Type: com.mysql.jdbc.CommunicationsException   Error Code: 0   SQL State: 08S01
Message:
   Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused

STACKTRACE:

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:519)
    at java.net.Socket.connect(Socket.java:469)
    at java.net.Socket.<init>(Socket.java:366)
    at java.net.Socket.<init>(Socket.java:208)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2744)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:154)
    at com.onseven.dbvis.wrapper.DriverManagerImpl.getConnection(Unknown Source)
    at com.onseven.dbvis.sql.N.İ(Unknown Source)
    at com.onseven.dbvis.sql.N.access$200(Unknown Source)
    at com.onseven.dbvis.sql.N$A.execute(Unknown Source)
    at se.pureit.util.ThreadCommand.runExecute(ThreadCommand.java:163)
    at se.pureit.util.ThreadCommand.run(ThreadCommand.java:143)
    at java.lang.Thread.run(Thread.java:619)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.

I started digging and here is what I dug out: by default on Slakware (and I guess on many other linuxes) MySQL is closed to TCP/IP connections (which Java uses to connect to the server), in order to enable networking you will have to comment the following line in your /etc/my.cnf:


skip-networking

And that worked for me.


First published on the old anydoby.com blog on 2008-03-29.