无法使用Docker Compose将SpringBoot连接到MySql

时间:2020-02-23 10:35:36

标签: mysql spring-boot docker docker-compose

我创建了一个docker compose文件,以将MySql连接到SpringBoot应用程序。但是我得到这个错误:

com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_242]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_242]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_242]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_242]

我的Dockerfile,我已经使用Maven安装创建了一个.jar文件:

FROM openjdk:8
EXPOSE 8083
ADD target/ProjectCrud.jar ProjectCrud.jar
ENTRYPOINT ["java", "-jar", "ProjectCrud.jar"]

docker-compose.yml:

version: '3'
    services:
    docker-mysql:
        restart: always
        container_name: docker-mysql
        image: mysql
        environment:
            MYSQL_DATABASE: pfa
            MYSQL_ROOT_PASSWORD: aze123qsd
            MYSQL_USER: root
            MYSQL_ROOT_HOST: '%'
        volumes:
            - ./sql:/docker-entrypoint-initdb.d
        networks:
            - spring-db

        ports:
            - "3306:3306"
        healthcheck:
            test: "/usr/bin/mysql --user=root --password=root--execute \"SHOW DATABASES;\""
        interval: 2s
        timeout: 20s
        retries: 10


    sign-up-app:
        build: ./
        expose:
            - "10222"
        ports:
            - 10222:10222
        environment:
            WAIT_HOSTS: mysql:3306
        depends_on:
            - docker-mysql
        networks:
            - spring-db
  networks:
      spring-db:

我的application.properties:

spring.datasource.username=root
spring.datasource.password=aze123qsd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://docker-mysql:3306/pfa? 
useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
server.port=8089

我尝试使用docker default ip在本地计算机上运行我的spring boot应用程序,而在此docker-compose.yml文件中仅运行MySql容器,并且运行良好。但是当我尝试docker撰写文件时。我收到此错误。

1 个答案:

答案 0 :(得分:0)

尝试一下

docker-compose.yml

version: '3'
    services:
      docker-mysql:
          restart: always
          container_name: docker-mysql
          image: mysql
          environment:
              MYSQL_DATABASE: pfa
              MYSQL_ROOT_PASSWORD: aze123qsd
              MYSQL_USER: root
              MYSQL_ROOT_HOST: '%'
          volumes:
              - ./sql:/docker-entrypoint-initdb.d
          networks:
              - spring-db
          ports:
              - "3306:3306"
          healthcheck:
              test: "/usr/bin/mysql --user=root --password=root--execute \"SHOW DATABASES;\""
              interval: 2s
              timeout: 20s
              retries: 10

      sign-up-app:
          build: ./
          expose:
              - 10222
          ports:
              - "10222:10222"
              - "8089:8089"   ## <-- Add this
          environment:
              WAIT_HOSTS: docker-mysql:3306   ## <-- change this
          depends_on:
              - docker-mysql
          networks:
              - spring-db

networks:
    spring-db: