使用 MySQL 和 Docker 的 Spring Boot:IllegalStateException:无法加载驱动程序类:com.mysql.cj.jdbc.Driver

Posted

技术标签:

【中文标题】使用 MySQL 和 Docker 的 Spring Boot:IllegalStateException:无法加载驱动程序类:com.mysql.cj.jdbc.Driver【英文标题】:Spring Boot with MySQL and Docker: IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver 【发布时间】:2020-03-12 22:45:43 【问题描述】:

我正在尝试使用 docker-compose 在 docker 容器中运行 Spring Boot 应用程序。

当我运行 docker-compose 时,我的 Spring Boot 应用程序会启动,尽管它给出了以下异常:

Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
    at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:222) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:174) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:85) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$e29a5a28.CGLIB$dataSource$0(<generated>) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$e29a5a28$$FastClassBySpringCGLIB$$181243b8.invoke(<generated>) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$e29a5a28.dataSource(<generated>) ~[spring-boot-autoconfigure-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
    ... 51 common frames omitted

这些是我的 Spring Boot 应用程序的 application.properties:

spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.datasource.url=jdbc:mysql://localhost:3306/onderdeeldb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root
#hibernate.hbm2ddl.auto=create

据我所知,我已经在 application.properties 和 pom.xml 中正确配置了 MySQL(休眠)。 我正在为我的 Spring Boot 应用程序使用以下 Dockerfile:

#### Stage 1: Build the application
FROM openjdk:11 as build

# Set the current working directory inside the image
WORKDIR /app

# Copy maven executable to the image
COPY mvnw .
COPY .mvn .mvn

# Copy the pom.xml file
COPY pom.xml .

# Build all the dependencies in preparation to go offline.
# This is a separate step so the dependencies will be cached unless
# the pom.xml file has changed.
RUN ./mvnw dependency:go-offline -B

# Copy the project source
COPY src src

# Package the application
RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

#### Stage 2: A minimal docker image with command to run the app
FROM openjdk:11

ARG DEPENDENCY=/app/target/dependency

# Copy project dependencies from the build stage
COPY --from=build $DEPENDENCY/BOOT-INF/lib /app/lib
COPY --from=build $DEPENDENCY/META-INF /app/META-INF
COPY --from=build $DEPENDENCY/BOOT-INF/classes /app

ENTRYPOINT ["java","-cp","app:app/lib/*","nl.contrivance.magenta.onderdeelservice.OnderdeelServiceApplication"]

为了启动 MySQL 容器和 spring boot docker app 容器,我正在运行以下 Docker compose 文件 (docker-compose up -d):

version: '3'
services:
  db:
    image: "mysql:8.0.18"
    container_name: "magenta_db"
    environment:
      - MYSQL_ROOT_PASSWORD=root
    ports:
      - "3306:3306"
  onderdeelservice:
    build: ./onderdeelservice
    environment:
      - SERVER_PORT=8282
      - SPRING_DATASOURCE_URL=jdbc:mysql://magenta_db:3306/onderdeeldb?createDatabaseIfNotExist=true
      - spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    ports:
      - "8282:8282"

这是我的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>nl.contrivance.magenta</groupId>
    <artifactId>onderdeelservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>OnderdeelService</name>
    <description>Microservice voor het beheren van onderdelen van een competitie</description>

    <properties>
        <java.version>11</java.version>
        <h2.version>1.4.199</h2.version>
        <mysql-connector-java.version>8.0.18</mysql-connector-java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>$mysql-connector-java.version</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <excludes>
                        <exclude>**/*IntegrationTest</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我不知道我做错了什么。 对于我的存储库,我扩展了 JpaRepository 接口。 我已经尝试了几乎所有的 *** 答案,但似乎没有任何帮助。 我错过了什么吗?

我们将不胜感激1

【问题讨论】:

尝试使用较低版本的 jdk,例如 8 或 9 @Sam 我已经尝试使用 Java 8 和 MySQL 连接器版本 5,以及使用 openjdk 8 的构建文件,仍然会导致相同的异常。 【参考方案1】:

原来我需要重建我的 docker 镜像... 我正在根据旧图像构建容器!

【讨论】:

以上是关于使用 MySQL 和 Docker 的 Spring Boot:IllegalStateException:无法加载驱动程序类:com.mysql.cj.jdbc.Driver的主要内容,如果未能解决你的问题,请参考以下文章

docker mysql镜像安装和使用

docker创建和使用mysql

docker compose 使用节点 js 和 mysql - 无法与 mysql 连接

使用 docker-compose 链接 django 和 mysql 容器

docker下MySQL修改配置

docker下MySQL修改配置