Connection Refused when initializing Hibernate with MySQL in docker-compose

Sunny Yu :

I have a docker-compose for a SpringBoot application with Hibernate and MySQL 8 server. But whenever my MySQL server and SpringBoot application are being brought up by docker-compose, I get:

HHH000342: Could not obtain connection to query metadata : Communications link failure
...
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: 
Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
...
Caused by: java.net.ConnectException: Connection refused

And here is my docker-compose.yaml:

version: '3'
services:
  my-account-microservice-db:
    container_name: my-account-microservice-db
    volumes:
      - /Users/syu/Desktop/my-mysql/storage/my-account-microservice-db:/var/lib/mysql
    image: mysql:8.0.19
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: my-account
    ports:
      - "3307:3306"
    networks:
      - my-network

  my-account-microservice:
    container_name: my-account-microservice
    image: my-account-microservice
    build:
      context: ./account-microservice
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://my-account-microservice-db:3307/my-account
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: password
      SPRING_DATASOURCE_DRIVER-CLASS-NAME: com.mysql.cj.jdbc.Driver
      # SPRING.DATASOURCE.URL: jdbc:mysql://my-account-microservice-db:3307/my-account
      # SPRING.DATASOURCE.USERNAME: root
      # SPRING.DATASOURCE.PASSWORD: password
      # SPRING.DATASOURCE.DRIVER-CLASS-NAME: com.mysql.cj.jdbc.Driver
      # DATABASE_HOST: my-account-microservice-db
      # DATABASE_USER: root
      # DATABASE_PASSWORD: password
      # DATABASE_PORT: 3307
    depends_on:
      - my-account-microservice-db
    ports:
      - "8090:8090"
    networks:
      - my-network
#    links:
#      - my-account-microservice-db:database

networks:
  my-network:

And my application.properties:

spring.application.name=account-microservice

server.port=8090

# ==============================================================
# = Data Source
# ==============================================================
# ******** Will probably put this on a central config server? would that work?
spring.datasource.url = jdbc:mysql://localhost:3307/my-account
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

# ==============================================================
# = Show or not log for each sql query
# ==============================================================
spring.jpa.show-sql = true

# ==============================================================
# = Keep the connection alive if idle for a long time (needed in production)
# ==============================================================
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ==============================================================
# = Hibernate ddl auto (create, create-drop, update)
# = The SQL dialect makes Hibernate generate better SQL for the chosen database
# ==============================================================
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl = true
spring.jpa.hibernate.ddl-auto = update

I have verified the following things:

  1. MySQL is up before the SpringBoot application is up
  2. Tried using -links, but didn't work
  3. Nothing is using port 3307
  4. I am able to connect to localhost:3307 with /bin/mysql -h0.0.0.0 -uroot -ppassword perfectly fine
  5. Having them on the same network
  6. Restarting Docker
  7. Make sure root user has privilege by running GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' on the server
  8. Running with individual DockerFiles
  9. Having correct mysql connector and Hibernate dependencies


But all the things I have tried above did not work.

*The weird thing is though, my SpringBoot application is able to connect to the mysql database if I just start the app by itself without using Docker (just using IntelliJ's SpringBoot config)

I have no idea what I am doing wrong. Please help ^

Burak Serdar :

You're exposing port 3306 of the database container on the host port 3307. However, the container port is still 3306. So when you connect to the database container, you should connect to port 3306

If you want to connect to your database container from the host you're running it on, use localhost:3307. That will not work for the containers running on your host though. For those, use container:3306.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=21233&siteId=1