Spring Boot cannot connect to MySQLand exits in Docker/Docker compose

前端 未结 2 1612
一整个雨季
一整个雨季 2020-12-06 23:13

Below is my application.properties file

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://loca         


        
相关标签:
2条回答
  • 2020-12-06 23:35

    Containers in compose services can connect to other containers by using name, e.g. try to ping database container from spring-boot container.

    Check if you can ping database container

    docker-compose exec spring-boot-jpa-docker-webapp /bin/bash
    

    once you are in the shell (You may have to install ping in spring-boot container for testing only apt-get install iputils-ping or yum install depending on image container is based on)

    ping docker-mysql
    

    if ping is successful then it means you can connect to database

    replace database url in application.properties with

    spring.datasource.url=jdbc:mysql://docker-mysql:3306/test?autoReconnect=true
    

    Minimal Example for Mysql

    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - "./volumes/database:/var/lib/mysql"
    

    Try following docker-compose

    version: "3.7"
    services:
      docker-mysql:
        image: mysql:latest
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        ports:
          - "3306:3306"
        environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_DATABASE=test
        volumes:
          - "./volumes/database:/var/lib/mysql"
        networks:
          - spring_net
      spring-boot-jpa-docker-webapp:
        image: springboot_docker
        ports:
          - "8080:8080"
        networks:
          - spring_net
    networks:
      spring_net:
    
    0 讨论(0)
  • 2020-12-06 23:43

    You are passing the DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USER and DATABASE_PASSWORD but you are not using it inside your app.

    Update your properties like this. (Better if your create a profile so that you app runs outside of docker)

    spring.datasource.url=jdbc:mysql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?autoReconnect=true
    spring.datasource.username=${DATABASE_USER}
    spring.datasource.password=${DATABASE_PASSWORD}
    

    If you really want to use localhost as your db_url. You can also use network_mode: "service:[service name]" property of docker compose. Only downside is that this property cannot be used with port property.

    version: '3'
    
    services: 
      docker-mysql:
        image: mysql:latest
        network_mode: "service:spring-boot-jpa-docker-webapp"
        environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_DATABASE=test
          - MYSQL_PASSWORD=root
      spring-boot-jpa-docker-webapp:
        image: springboot_docker
        depends_on:
          - docker-mysql
        ports:
          - 8080:8080
          - 3306 #Add this only if you want to expose the mysql to outer world.
        environment:
          - DATABASE_USER=root
          - DATABASE_PASSWORD=root
          - DATABASE_NAME=test
    

    And your properties file could look like

    spring.datasource.url=jdbc:mysql://localhost:3306/${DATABASE_NAME}?autoReconnect=true
    spring.datasource.username=${DATABASE_USER}
    spring.datasource.password=${DATABASE_PASSWORD}
    
    0 讨论(0)
提交回复
热议问题