Issue
I'm having trouble connecting my spring application with the MySQL database. In particular, I report the code of the docker compose, the docker file, application.yml and the error log.
docker-compose:
version: '3.3'
services:
my-sql-db:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=user
- MYSQL_USER=user
- MYSQL_DATABASE=spring-app
ports:
- "3306:3306"
customerservice:
image: customer-service
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
depends_on:
- my-sql-db
environment:
SPRING_PROFILES_ACTIVE: MySQLdocker
Dockerfile:
FROM maven:3.6.3-jdk-8
COPY ./ ./
RUN mvn clean package
EXPOSE 8080
CMD ["java", "-jar", "target/my-spring-project-0.0.1-SNAPSHOT.jar"]
Application.yml:
spring:
jpa:
open-in-view: false #per levare warnig OpenSessionInView
properties:
hibernate:
jdbc:
time_zone: CET
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: none
datasource:
url: jdbc:mysql://my-sql-db:3306/spring-app?createDatabaseIfNotExist=true
username: user
password: user
config:
activate:
on-profile: MySQLdocker
Executing the command "docker-compose up -d" the output error is:
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:174) ~[mysql-connector-java-8.0.27.jar!/:8.0.27] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.27.jar!/:8.0.27]
Solution
I'm not seeing MYSQL_PASSWORD
defined in your docker-compose file.
Generally, the error suggests the app cannot connect to MySQL at all. When you have such a problem that could be caused by many things, you should try and narrow it down. Check if the DB is up and running. If yes, then try to connect to it from some MySQL client. And so on, until you figure out what exactly went wrong or at least come up with a more specific question.
Answered By - at54321