skip to Main Content

I have a products db configured on docker:

  productsdb:
container_name: productsdb 
volumes:
  - productsdb_data1:/var/lib/mysql
image: mysql:5.7
environment:
  MYSQL_ROOT_PASSWORD: "admin"
  MYSQL_PASSWORD: "admin"
networks:
  - commerce_net  
ports:
  - "3309:3306"  

When I try to connect to this db from my spring boot project, the following exception is shown during server startup:

    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) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    ... 33 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
    ... 36 common frames omitted

The application.properties are set to connect to host ‘productsdb’ as:

spring.datasource.url=jdbc:mysql://productsdb:3309/products?createDatabaseIfNotExist=true&useSSL=false

The server start was tried using docker compose as well as using the command:

 docker run --network=commerce_net  -p 8085:8085 producttest -d

Any possible reason why this exception could occur?

2

Answers


  1. If you want to start your application in the same container as your database, you must use the container port instead of using the host port of your database. So the database address you use in the application should be:

    spring.datasource.url=jdbc:mysql://productsdb:3306/products?createDatabaseIfNotExist=true&useSSL=false
    

    The port definition in docker-compose.yml is made in the form of HOST:CONTAINER. That is why your first port refers to your host port, and the second one refers to your container port. You can access detailed information from the Docker documentation.

    Login or Signup to reply.
  2. If springboot is also in same docker compose file then use MySQL port as 3306

    If its in different docker compose file then please update your network to --network={db_compose_file_directory_name}_commerce_net and spring.datasource.url=jdbc:mysql://localhost:3309/products?createDatabaseIfNotExist=true&useSSL=false

    Try to provide docker-compose file and complete docker run command for service

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search