Maciej
Maciej

Reputation: 552

SpringBoot on Docker unable to connect to MySQL

I have written docker compose for my SpringBoot app with MySQL. When I run docker-compose up eventually I get an error:

 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
      at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:325) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
      ... 69 common frames omitted
  Caused by: com.mysql.cj.jdbc.exceptions.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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
      at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
      at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
      at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
      at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
      at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) ~[mysql-connector-java-8.0.11.jar!/:8.0.11]
      at java.sql.DriverManager.getConnection(DriverManager.java:664) ~[na:1.8.0_111]
      at java.sql.DriverManager.getConnection(DriverManager.java:208) ~[na:1.8.0_111]
      at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
      at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
      at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:196) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
      at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:159) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
      at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
      at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.3.17.RELEASE.jar!/:4.3.17.RELEASE]
      ... 70 common frames omitted
  Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

At first I thought the the cause was the db but I managed to run local instance of the app and connect to the docker mysql. Next I assumed that the spring app tries to connect to mysql before it is up and running but I restarted the app (mysql was already running) and got the same error. Currently I think that this might be caused by the windows10 firewall but I am running low on ideas.

Dockerfile:

FROM java:8
LABEL maintainer="maciej"
WORKDIR /app
COPY target/kamienica.jar /app/kamienica.jar
ENTRYPOINT ["java", "-jar","kamienica.jar", "--spring.profiles.active=docker"]

docker-compose.yml

version: '3'

services:
  mysql-docker-container:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=maciej
      - MYSQL_DATABASE=kamienica
      - MYSQL_USER=maciej
      - MYSQL_PASSWORD=maciej
    volumes:
      - /data/mysql
    ports:
    - 3333:3306
    container_name: kamienica-db
  kamienica-app:
    image: kamienica-image
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
      - mysql-docker-container
    ports:
      - 8080:8080
    volumes:
      - /data/spring-boot-app
    container_name: kamienica-app

and the application-docker.properties:

jdbc.url = jdbc:mysql://kamienica-db:3333/kamienica?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
jdbc.username = maciej
jdbc.password = maciej
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto=create-drop
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.current_session_context_class=thread
hibernate.connection.characterEncoding=UTF-8
hibernate.connection.charSet=UTF-8

Upvotes: 2

Views: 16975

Answers (2)

sampath pallekumbura
sampath pallekumbura

Reputation: 155

jdbc:mysql://[mysql-docker-container-name]:[mysql-port]/[db-name]

in your docker-compose.yml mysql-docker-container-name is mysql-docker-container

so change your spring boot application.properties file mysql url as following

jdbc:mysql://mysql-docker-container:3306/kamienica

Upvotes: 2

toto
toto

Reputation: 709

Try to change the url into this your properties file

jdbc:mysql://mysql-docker-container:3306/kamienica

Upvotes: 9

Related Questions