'how to run spring boot with mysql by docker compose?

I haven't been able to find a solution to this error for over a week.

applicatio.yml

spring:
  jpa:
    hibernate:
      ddl-auto: create-drop
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL8Dialect
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://test-mysql:3306
    username: root
    password: 1234

and docker-compose yml.


version: "3.7"

services:
  test-mysql:
    container_name: test-mysql
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: 1234
    volumes:
      - ./data.sql:/docker-entrypoint-initdb.d/data.sql
    ports:
      - "3306:3306"
    networks:
      - test-mysql
  app:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - test-mysql
    container_name: app
    networks:
      - test-mysql

networks:
  test-mysql:
 

data.sql

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

If you run the script like this, an error occurs when running the Spring Boot application. I can't seem to connect to mysql, but I'm not sure what the problem is.

// error logs
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.28.jar!/:8.0.28]


...

2022-04-09 07:53:26.424  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
Hibernate: drop table if exists simple
2022-04-09 07:53:27.949  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-04-09 07:53:28.956 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

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.

Caused by: java.net.ConnectException: Connection refused

For reference, I use lima instead of docker desktop, but I don't know if this is the cause.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source