Spring Boot FlywayException:无法连接到数据库.配置url、用户和密码 [英] Spring Boot FlywayException: Unable to connect to the database. Configure the url, user and password
问题描述
当我运行 maven flyway:migrate
时,出现错误
未能执行目标 org.flywaydb:flyway-maven-plugin:6.5.5:migrate(default-cli) 在项目 myProject 上: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
弹簧:数据源:driverClassName: org.postgresql.Driverurl: "jdbc:postgresql://localhost:5433/myDB";用户名:postgres密码:test123
依赖:
<依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jooq</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><范围>测试</范围></依赖><依赖><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>42.2.16</version></依赖><依赖><groupId>org.flywaydb</groupId><artifactId>flyway-core</artifactId><version>7.0.0</version></依赖>
插件:
<groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件><插件><groupId>org.flywaydb</groupId><artifactId>flyway-maven-plugin</artifactId><version>6.5.5</version></插件>
Run With maven:
<块引用>未能执行目标 org.flywaydb:flyway-maven-plugin:6.5.5:migrate(default-cli) 在项目 myProject 上:org.flywaydb.core.api.FlywayException:无法连接到数据库.配置网址、用户和密码!
您可以在 flyway-maven-plugin 配置中配置 url、用户和密码 参见 Maven 第一步
<groupId>org.flywaydb</groupId><artifactId>flyway-maven-plugin</artifactId><version>7.0.0</version><配置><url>jdbc:postgresql://localhost:5433/myDB</url><user>postgres</user><password>test123</password></配置></插件>
或使用环境变量:
mvn flyway:migrate -Dflyway.url=jdbc:postgresql://localhost:5433/myDB -Dflyway.user=postgres -Dflyway.password=test123
更多方法在 https://www.baeldung.com/database-migrations-带飞路
使用 spring-boot 运行:
<块引用>Spring Boot 在应用中自动配置和触发 Flyway当您将 Flyway 核心库包含到项目中时启动.看@ConditionalOnClass(Flyway.class)
在Flyway自动配置:
@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 })公共类 FlywayAutoConfiguration {...}
使用 mvn spring-boot:run
或 java -jar app.jar
来运行应用程序
NB :还要检查迁移脚本是否在 db/migration
中,否则提供带有 spring.flyway.locations
属性的位置
资源:
https://flywaydb.org/documentation/configuration/parameters/>
https://flywaydb.org/documentation/getstarted/firststeps/maven/一个>
When I run maven flyway:migrate
, I get the error
Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli) on project myProject: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
I have my Spring Boot settings in my application.yml file, but I guess the error means it doesn't detect the database config. This documention says, "Spring Boot will then automatically autowire Flyway with its DataSource and invoke it on startup." If I add the configuration to my pom.xml in the flyway plugin section, it connects to the database successfully, but I want it to use my application.yml config. Not the pom.xml. So what am I doing wrong?
Link to repo with issue: https://github.com/jack-cole/BrokenSpringBoot
application.yml
spring:
datasource:
driverClassName: org.postgresql.Driver
url: "jdbc:postgresql://localhost:5433/myDB"
username: postgres
password: test123
Dependencies:
<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>
Plugins:
<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 With maven:
Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli) on project myProject: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
You can configure url, user and password in flyway-maven-plugin configuration see 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>
or with environment variables:
mvn flyway:migrate -Dflyway.url=jdbc:postgresql://localhost:5433/myDB -Dflyway.user=postgres -Dflyway.password=test123
More approaches in https://www.baeldung.com/database-migrations-with-flyway
Run with spring-boot:
Spring Boot auto configure and trigger Flyway at the application startup when you include the Flyway core library into the project. See usage of
@ConditionalOnClass(Flyway.class)
in 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 { ... }
Use mvn spring-boot:run
or java -jar app.jar
to run the application
NB : Check also that migration scripts are in db/migration
otherwise provide the locations with spring.flyway.locations
property
Resources:
https://flywaydb.org/documentation/configuration/parameters/
https://flywaydb.org/documentation/getstarted/firststeps/maven/
这篇关于Spring Boot FlywayException:无法连接到数据库.配置url、用户和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!