Spring Boot FlywayException: 无法连接到数据库。请配置url、user和password。

huangapple go评论68阅读模式
英文:

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、user 和 password!

我在 application.yml 文件中配置了 Spring Boot 设置,但我猜这个错误意味着它无法检测到数据库配置。这个文档 表示,"Spring Boot 将自动使用其 DataSource 自动连接 Flyway,并在启动时调用它。" 如果我将配置添加到 pom.xml 的 flyway 插件部分,它能成功连接到数据库,但我希望它使用我的 application.yml 配置,而不是 pom.xml。那么我做错了什么?

问题仓库链接:https://github.com/jack-cole/BrokenSpringBoot

application.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>
英文:

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>

答案1

得分: 20

使用 Maven 运行:

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

您可以在 flyway-maven-plugin 的配置中配置 url、user 和 password,请参阅 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。请参阅 FlywayAutoConfiguration 中的 @ConditionalOnClass(Flyway.class) 的使用:

@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

英文:

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

&lt;plugin&gt;
    &lt;groupId&gt;org.flywaydb&lt;/groupId&gt;
    &lt;artifactId&gt;flyway-maven-plugin&lt;/artifactId&gt;
    &lt;version&gt;7.0.0&lt;/version&gt;
    &lt;configuration&gt;
        &lt;url&gt;jdbc:postgresql://localhost:5433/myDB&lt;/url&gt;
        &lt;user&gt;postgres&lt;/user&gt;
        &lt;password&gt;test123&lt;/password&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

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/

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

答案2

得分: 9

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

Flyway Maven 插件并不了解 Spring Boot 的配置,它只考虑以下来源:覆盖顺序

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

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

英文:

You quoted a part of spring boot docs, but you launched your migration not by spring boot, but as a maven task.

Flyway maven plugin is not aware of spring boot configuration, it only takes the following sources into account: Overriding order

  1. System properties
  2. Environment variables
  3. Custom config files
  4. Maven properties
  5. Plugin configuration section
  6. Credentials from settings.xml
  7. <user-home>/flyway.conf
  8. Flyway Maven plugin defaults

On my PC, I used the environment variables approach - I have the same environment variables defined for build plugin and for spring boot.

答案3

得分: 0

如果您希望在启动应用程序时(使用spring-boot),只需移除 flyway-maven-plugin 插件,您无需它。

-- 编辑

顺便提一下,如果您运行 maven flyway:migrate,实际上需要在插件上设置凭据(它无法访问java资源)。

  • 因此,您需要运行此任务 => 您必须在 pom.xml 中的插件中设置凭据。
  • 或者您不需要它,因此只需删除该插件,并在 application.yaml 文件中为应用程序启动设置凭据。
英文:

If you want Flyway to be executed when launching your app (with spring-boot), just remove the flyway-maven-plugin plugin, you don't need it.

-- EDIT

Btw, if you run maven flyway:migrate, you will indeed need to have the credentials set on the plugin (it isn't accessing the java resources).

  • So either you need to run this task => you have to set the credentials on in the plugin on pom.xml.
  • Or you don't need it, so you just remove the plugin and set the credentials on the application.yaml file for your application startup

答案4

得分: 0

你试过吗?

application.yml

spring:
    flyway:
        driverClassName: org.postgresql.Driver
        url: "jdbc:postgresql://localhost:5433/myDB"
        user: postgres
        password: test123
        enabled: true
英文:

Have you tried?

application.yml

spring:
    flyway:
        driverClassName: org.postgresql.Driver
        url: &quot;jdbc:postgresql://localhost:5433/myDB&quot;
        user: postgres
        password: test123
        enabled: true

答案5

得分: 0

你的配置有一些遗漏的地方:

  • 你在Spring Boot的.yml配置文件中使用了过多的空格。每个下一级应该比上一级多两个空格,像这样:
spring:
  datasource:
    url: jdbc:postgresql://localhost/myDB
    username: postgres
    password: root

不要再多加了。否则,它将无法正常工作!

如果你使用的是IntelliJ Idea,你可以使用自动格式化快捷键来格式化.yml:<kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>L</kbd>。

或者你可以使用与.properties相同的样式:

spring.datasource.url: ...
spring.datasource.username: ...

更加冗余。然而,出错的机会较小。

  • 你正在使用Spring Boot。因此,你不需要直接包含版本。更好的做法是使用Spring的依赖管理:
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>

以及

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
</plugin>

对于大多数情况,你可以确信这些版本的组合会很好地工作。

  • 你应该将迁移文件放在resources/db/migration目录下。同时命名也非常重要。第一个文件的名称应该类似于:

> V1__Init_Db.sql

默认情况下,在V1后面要有两个下划线,变成V1__

这里有更多信息:迁移信息

  • 此外,检查一下你的配置文件是否需要其他值,例如:
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: create
英文:

Your configuration has a few missed moments:

  • you use too many spaces for Spring Boot's .yml configuration file. Every next level should contain 2 more spaces, like:
spring:
  datasource:
    url: jdbc:postgresql://localhost/myDB
    username: postgres
    password: root

No more. Otherwise, it would not work!

If you using IntelliJ Idea you could use autoformat shortcut for formatting .yml: <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>L</kbd>.

Or you could use the same style as for .properties:

spring.datasource.url: ...
spring.datasource.username: ...

More redundant. However, less chance to make it incorrect.

  • you are using Spring Boot. Thus, you don't need to include the version directly.
    Much better will be left to Spring's dependency management:
&lt;dependency&gt;
    &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
    &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
    &lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.flywaydb&lt;/groupId&gt;
    &lt;artifactId&gt;flyway-core&lt;/artifactId&gt;
&lt;/dependency&gt;

And

&lt;plugin&gt;
    &lt;groupId&gt;org.flywaydb&lt;/groupId&gt;
    &lt;artifactId&gt;flyway-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;

For most cases, you could be sure that such a combination of versions will work fine.

  • you should put your migration file under resources/db/migration. Also naming is very important. The first file should be something like:

> V1__Init_Db.sql

By default exactly two underscores after V1 -> V1__ !

Here more info: Migrations information.

  • Also, check if you need additional values for your config file, like:
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: create

huangapple
  • 本文由 发表于 2020年10月4日 14:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64191935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定