java - 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>
解决方案
使用 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:run
或java -jar app.jar
运行应用程序
注意:还要检查迁移脚本是否在db/migration
其他位置提供具有spring.flyway.locations
属性的位置
资源:
https://flywaydb.org/documentation/configuration/parameters/
https://flywaydb.org/documentation/getstarted/firststeps/maven/
推荐阅读
- python - 如何比较和匹配来自同一数据框不同列的数据
- azure - Azure Servicebus 锁定删除
- .net - OpenTelemetry 收集器导出到 Prometheus 不起作用
- javascript - 使用 node-mocks-http 包测试 next.js api 路由
- python - 用于计算复利百分比的 Python 函数
- python - 如何在 1d 列表中组合相同的匹配项并使其成为 2d 列表
- flutter - Flutter:同屏英雄动画
- ios - Blob 下载不适用于 iOS Chrome 或 Firefox
- visual-studio-code - VS Code 自动为源文件设置许可证头
- boto3 - AWS BATCH - SubmitJob 操作:作业队列附加到无法运行具有 EC2 功能的作业的计算环境