Search code examples
mysqlspring-boot

How to connect to a remote MySQL server from a spring boot application


Problem:

I want to connect my spring boot application to a database located in a remote MySql server, but it says "connection refused", you can find the details of the error in following section.

The error:

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
    at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:850)
    ... 50 more
Caused by: java.net.ConnectException: Connection refused: no further information
    at java.base/sun.nio.ch.Net.pollConnect(Native Method)
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    ... 52 more

Note: I can access the same database from my computer/system via mysql workbench with same credentials.

Steps taken:

  1. Created a database user for remote access as remoteUser@%.
  2. Granted full permissions to the newly created user on all databases.
  3. flushed privileges.
  4. Allowed incoming traffic on port 3308 sudo ufw allow 3308.
  5. updated the mysqld.cnf file.

#commented both of the following lines
# bind-address      = 127.0.0.1
# mysqlx-bind-address   = 127.0.0.1

  1. restarted the mysql server sudo service mysql restart.
  2. updated connection string in spring boot app properties file.
 spring.datasource.url=jdbc:mysql://server_ip:3308/db_name
 spring.datasource.username=remoteUser
 spring.datasource.password=remotePassword
  1. restarted the spring boot application.

The issue has not been solved, and need your help.


Solution

  • I have the issue by adding just one line to the Mysqld.cnf file.

    bind-address      = 0.0.0.0
    

    that was all needed.