[fix] Java JDBC ConnectException: Connection refused

Post Banner
Caused by: java.net.ConnectException: Connection refused (Connection refused)
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
 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:607)
 at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)
 at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
 ... 9 more

If your Application is not able to connect to the Database you will get ConnectException: Connection refused while working with Java JDBC, below are a few reasons that can cause this Exception,

Some Solutions/Fix

  1. The Database is down? Check connection to the database via DB Client or Console. Try doing telnet.
  2. Make sure you have defined the right database Hostname/IP Address in your JDBC DriverManager Connection.
  3. Make sure the Port is correct.
  4. Make sure your firewall settings are not restricting access.
  5. Make sure the Database server if having an IP Whitelisting policy, your IP is listed.
  6. If the Database Server is on Cloud (AWS/Azure/GCS), the right policies are set.
  7. Check that you have Internet connectivity.

Have Questions? Post them here!