Spring Boot FlywayException:无法连接到数据库。配置url、用户和密码

Jac*_*ole 11 java maven flyway spring-boot

当我运行时maven flyway:migrate,我收到错误

无法在项目 myProject 上执行目标 org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli): org.flywaydb.core.api.FlywayException: 无法连接到数据库。配置url,用户和密码!

我的 application.yml 文件中有我的 Spring Boot 设置,但我猜这个错误意味着它没有检测到数据库配置。该文档说,“然后,Spring Boot 将自动使用其数据源自动装配 Flyway,并在启动时调用它。” 如果我将配置添加到我的 pom.xml 中的 flyway 插件部分,它会成功连接到数据库,但我希望它使用我的 application.yml 配置。不是 pom.xml。那么我做错了什么?

链接到有问题的回购:https : //github.com/jack-cole/BrokenSpringBoot

应用程序.yml

spring:
    datasource:
        driverClassName: org.postgresql.Driver
        url: "jdbc:postgresql://localhost:5433/myDB"
        username: postgres
        password: test123
Run Code Online (Sandbox Code Playgroud)

依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</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-jooq</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.16</version>
</dependency>
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>7.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

插件:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>6.5.5</version>
</plugin>
Run Code Online (Sandbox Code Playgroud)

Les*_*iak 10

您引用了 Spring Boot 文档的一部分,但您不是通过 Spring Boot 启动迁移,而是作为 Maven 任务启动。

Flyway maven 插件不知道 spring boot 配置,它只考虑以下来源:覆盖顺序

  1. 系统属性
  2. 环境变量
  3. 自定义配置文件
  4. Maven 属性
  5. 插件配置部分
  6. 来自settings.xml 的凭证
  7. /flyway.conf
  8. Flyway Maven 插件默认值

在我的电脑上,我使用了环境变量方法 - 我为构建插件和 Spring Boot 定义了相同的环境变量。

  • Flyway 应该在启动 Spring Boot 时运行,因为它是自动配置的。使用 mvn spring-boot:run 从 Maven 启动 Spring Boot 应用程序。Spring Boot 有更多用于 Flyway 配置的属性:https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-migration-properties (2认同)

Iss*_*TIF 6

使用 Maven 运行:

无法在项目 myProject 上执行目标 org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli): org.flywaydb.core.api.FlywayException: 无法连接到数据库。配置url,用户和密码!

您可以在 flyway-maven-plugin 配置中配置url、用户和密码,参见First Steps Maven

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>7.0.0</version>
    <configuration>
        <url>jdbc:postgresql://localhost:5433/myDB</url>
        <user>postgres</user>
        <password>test123</password>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

或使用环境变量:

mvn flyway:migrate -Dflyway.url=jdbc:postgresql://localhost:5433/myDB -Dflyway.user=postgres -Dflyway.password=test123
Run Code Online (Sandbox Code Playgroud)

https://www.baeldung.com/database-migrations-with-flyway 中的更多方法

使用 spring-boot 运行:

当您将 Flyway 核心库包含到项目中时,Spring Boot 会在应用程序启动时自动配置和触发 Flyway。查看@ConditionalOnClass(Flyway.class)FlywayAutoConfiguration 中的用法:

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Flyway.class)
@Conditional(FlywayDataSourceCondition.class)
@ConditionalOnProperty(prefix = "spring.flyway", name = "enabled", matchIfMissing = true)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class,
      HibernateJpaAutoConfiguration.class })
@Import({ FlywayEntityManagerFactoryDependsOnPostProcessor.class, FlywayJdbcOperationsDependsOnPostProcessor.class,
      FlywayNamedParameterJdbcOperationsDependencyConfiguration.class })
public class FlywayAutoConfiguration {
    ...
}
Run Code Online (Sandbox Code Playgroud)

使用mvn spring-boot:runjava -jar app.jar运行应用程序

注意:还要检查迁移脚本是否在,db/migration否则为位置提供spring.flyway.locations属性

资源:

https://flywaydb.org/documentation/configuration/parameters/

https://flywaydb.org/documentation/getstarted/firststeps/maven/

https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-execute-flyway-database-migrations-on-startup