为什么我的应用程序未连接到mysql?

时间:2019-09-09 17:38:53

标签: mysql docker docker-compose dockerfile

我做了docker-compose,但是我的应用程序不想连接到数据库。可能是什么问题呢? 该应用程序本身可以很好地启动,但是一旦将其变成一个容器,它就不会连接到数据库,并且会发生很多错误。

docker-compose:

version: '3.7'

services:

  db:
    image: mysql:8.0.17
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 123test321
    ports:
      - 3306:3306
    networks:
      - employee-mysql
    volumes:
      - /home/alexey/temp/mysql01:/var/lib/mysql

  adminer:
    image: adminer
    restart: always
    ports:
      - 3380:8080

  webapp:
    build:
      context: .
    restart: always
    ports:
      - 80:8080
    networks:
      - employee-mysql
    depends_on:
      - db

networks:
  employee-mysql:

这是dockerfile:

FROM anapsix/alpine-java:8_jdk

FROM openjdk:8
ADD target/webapp-1.0.1.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

这是我的日志:

    Starting work_db_1      ... done
Starting work_adminer_1 ... done
Starting work_webapp_1  ... done
Attaching to work_db_1, work_adminer_1, work_webapp_1
db_1       | 2019-09-09T17:29:20.983653Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
db_1       | 2019-09-09T17:29:20.983965Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17) starting as process 1
db_1       | 2019-09-09T17:29:22.697164Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1       | 2019-09-09T17:29:22.704622Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1       | 2019-09-09T17:29:22.807613Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
db_1       | 2019-09-09T17:29:23.019957Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
adminer_1  | PHP 7.3.9 Development Server started at Mon Sep  9 17:29:21 2019
webapp_1   | [INFO ] 2019-09-09 17:29:30.149 [main] ApplicationKt - Starting ApplicationKt v1.0.1 on d52aa0a7ff7e with PID 1 (/app.jar started by root in /)
webapp_1   | [INFO ] 2019-09-09 17:29:30.184 [main] ApplicationKt - No active profile set, falling back to default profiles: default
webapp_1   | [INFO ] 2019-09-09 17:29:38.634 [main] RepositoryConfigurationDelegate - Bootstrapping Spring Data repositories in DEFAULT mode.
webapp_1   | [INFO ] 2019-09-09 17:29:39.562 [main] RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 894ms. Found 19 repository interfaces.
webapp_1   | [INFO ] 2019-09-09 17:29:42.756 [main] PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$e8bed525] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
webapp_1   | [INFO ] 2019-09-09 17:29:43.087 [main] PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$816c9d5f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
webapp_1   | [INFO ] 2019-09-09 17:29:43.130 [main] PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
webapp_1   | [INFO ] 2019-09-09 17:29:43.139 [main] PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@7d61eb55' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
webapp_1   | [INFO ] 2019-09-09 17:29:43.159 [main] PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$a6414011] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
webapp_1   | [INFO ] 2019-09-09 17:29:43.202 [main] PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.security.config.annotation.method.configuration.Jsr250MetadataSourceConfiguration' of type [org.springframework.security.config.annotation.method.configuration.Jsr250MetadataSourceConfiguration$$EnhancerBySpringCGLIB$$cb965827] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
webapp_1   | [INFO ] 2019-09-09 17:29:43.237 [main] PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'jsr250MethodSecurityMetadataSource' of type [org.springframework.security.access.annotation.Jsr250MethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
webapp_1   | [INFO ] 2019-09-09 17:29:43.244 [main] PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
webapp_1   | [INFO ] 2019-09-09 17:29:45.329 [main] HikariDataSource - HikariPool-1 - Starting...
webapp_1   | [ERROR] 2019-09-09 17:29:46.161 [main] HikariPool - HikariPool-1 - Exception during pool initialization.
webapp_1   | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
webapp_1   | 
webapp_1   | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

1 个答案:

答案 0 :(得分:0)

好的。

version: '3.7'

services:

  db:
    image: mysql:8.0.17
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ****
    ports:
      - 3306:3306
    volumes:
      - /home/alexey/temp/mysql01:/var/lib/mysql

  adminer:
    image: adminer
    restart: always
    ports:
      - 3380:8080

  webapp:
    build:
      context: .
    environment:
      CONTEXT_PATH: /a-test
      DB_HOST: db
      DB_PORT: 3306
      DB_NAME: ****
      DB_USER: ****
      DB_PASSWORD: ****
      MAIL_LOGIN: ****
      MAIL_PASSWORD: ****
    ports:
      - 80:8080
    depends_on:
      - db

和Dockerfile:

FROM anapsix/alpine-java:8_jdk

FROM openjdk:8

ADD target/webapp-*.jar webapp.jar

EXPOSE 8080

ENTRYPOINT java -jar webapp.jar