You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Flyway与MongoDB自动迁移失效问题排查

Spring Boot + Flyway + MongoDB 自动迁移未触发问题

我在Spring Boot项目中尝试整合Flyway与MongoDB,之前在SQL数据库项目里Spring能自动执行Flyway迁移,但在MongoDB场景下,Spring Boot的自动配置似乎没有正常初始化FlywayMigrationInitializer。

我已按照官方文档添加相关依赖,也参考过相关配置示例,但迁移仍未自动执行。

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>3.3.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>mongo-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mongo-app</name>
    <description>mongo-app</description>

    <properties>
        <java.version>17</java.version>
        <flyway.version>10.21.0</flyway.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>${flyway.version}</version>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-database-mongodb</artifactId>
            <version>${flyway.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.github.kornilova203</groupId>
                    <artifactId>mongo-jdbc-driver</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.kornilova203</groupId>
            <artifactId>mongo-jdbc-driver</artifactId>
            <version>1.19</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/mongo-jdbc-standalone-1.19.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml 配置

spring:
  application:
    name: mongo-app
  data:
    mongodb:
      uri: mongodb://localhost:27017
      database: example
  datasource:
    url: jdbc:mongodb://localhost:27017/example
    driver-class-name: com.dbschema.MongoJdbcDriver
  flyway:
    url: jdbc:mongodb://localhost:27017/example
    driver-class-name: com.dbschema.MongoJdbcDriver
    environment: "mongodb"
    fail-on-missing-locations: true
    locations: classpath:db/migration
    sqlMigrationSuffixes:
      - ".js"
    enabled: true

请问我还缺少哪些配置才能让Flyway迁移自动运行?

内容的提问来源于stack exchange,提问作者Cory

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 10:24:57