gpt4 book ai didi

mysql - Docker-compose部署了我的应用程序,但未加载init.sql脚本数据

转载 作者:行者123 更新时间:2023-11-29 15:43:13 25 4
gpt4 key购买 nike

我正在使用Windows,Docker Desktop版本19.03.1,内部版本74b1e89和docker-compose版本1.24.1,内部版本4667896b

我的docker机器是hyper-v

我正在尝试为我的spring jpa应用程序的部署初始化mysql数据库docker容器。

我有一个带有两项服务的docker-compose.yml文件:mysql-docker-container和spring-jpa-app。该文件位于项目内的/ resources /文件夹中。
init.sql文件也位于此处。

我将init.sql与application.properties放在同一文件夹中,因为我是通过以下配置读取的:

spring.datasource.initialization-mode=always
spring.datasource.type=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
spring.jpa.hibernate.ddl-auto=none


并排除HikariCP:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>


它将在init.sql中加载所有数据库和数据

但是,相反,我遇到了通讯链接故障,但仍然启动了该应用程序。

邮递员工作并给出响应,但没有我要加载的数据,结果为空。

这就是为什么我包括运行状况检查以检查mysql容器是否“健康”的原因,但是我不确定那是我所需要的。

另外,我尝试以管理员身份在另一个git bash中输入mysql容器(在docker-compose up --build之后启动应用程序时),但我无法访问它,因为docker ps -a为空

docker-compose-yml

version: '3.7'
services:

mysql-docker-container:
image: mysql
networks:
- mired
container_name: mysql-docker-container
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=database
- MYSQL_USER=user
- MYSQL_PASSWORD=user
ports:
- 2012:3306
volumes:
- c:/Users/User/Documents/project/src/main/resources/db/:/docker-entrypoint-initdb.d
tty: true
healthcheck:
test: ["CMD-SHELL", 'mysqladmin ping']
interval: 10s
timeout: 2s
retries: 10

spring-jpa-app:
build:
context: .
dockerfile: Dockerfile
networks:
- mired
container_name: spring-jpa-app
links:
- mysql-docker-container:mysql-docker-container
depends_on:
- mysql-docker-container
ports:
- 8087:8080
tty: true

networks:
mired:


Docker文件


FROM java:8
VOLUME /tmp
EXPOSE 8080
ADD app.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]


application.properties

server.port=8080
spring.datasource.url=jdbc:mysql://172.18.67.41:3306/database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=user
spring.datasource.initialization-mode=always
spring.datasource.type=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
spring.jpa.hibernate.ddl-auto=none
logging.file=log/log.log
logging.level.org.springframework=info
logging.level.orghibernate=info


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.app</groupId>
<artifactId>app</artifactId>
<packaging>jar</packaging>

<name>app</name>
<description>Application</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>auth0-spring-security-api</artifactId>
<version>1.0.0-rc.3</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>


spring-jpa-app            | 2019-08-05 10:23:44.920 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
spring-jpa-app |
spring-jpa-app | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
spring-jpa-app |
spring-jpa-app | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

最佳答案

您应该使用docker service name而不是静态ip

关于mysql - Docker-compose部署了我的应用程序,但未加载init.sql脚本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57357211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com