首页 > 解决方案 > Spring Boot FlywayException:无法连接到数据库。配置url、用户和密码

问题描述

当我运行时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 与其 DataSource 自动连接并在启动时调用它。” 如果我在 flyway 插件部分将配置添加到我的 pom.xml 中,它会成功连接到数据库,但我希望它使用我的 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

依赖项:

<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>

插件:

<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>

标签: javaspring-bootmavenflyway

解决方案


使用 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>

或使用环境变量:

mvn flyway:migrate -Dflyway.url=jdbc:postgresql://localhost:5433/myDB -Dflyway.user=postgres -Dflyway.password=test123

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 {
    ...
}

使用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


推荐阅读