SpringBoot - Flyway - JPA 集成 - 创建名称为“flywayInitializer”的 bean 时出错 - information_schema 中的未知表“事件”

     2023-03-28     140

关键词:

【中文标题】SpringBoot - Flyway - JPA 集成 - 创建名称为“flywayInitializer”的 bean 时出错 - information_schema 中的未知表“事件”【英文标题】:SpringBoot - Flyway - JPA integration -- Error creating bean with name 'flywayInitializer' - Unknown table 'events' in information_schema 【发布时间】:2018-07-23 15:30:59 【问题描述】:

我正在使用JPAMySql 作为数据库将我的Spring Boot ApplicationFlyway 集成。

Git 源 - https://github.com/codethinker09/codeExamples.git

项目 - flywaydemo

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/spring-jpa-flyway?autoReconnect=true&zeroDateTimeBehavior=convertToNull
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.username = root
spring.datasource.password = root123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

flyway.password = root123
flyway.url= jdbc:mysql://localhost:3306/spring-flyway?autoReconnect=true&zeroDateTimeBehavior=convertToNull
flyway.user = root
flyway.enabled=true

分级

buildscript 
    ext 
        springBootVersion = '1.5.10.RELEASE'
    
    repositories 
        mavenCentral()
    
    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
    


apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.boot.flyway'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories 
    mavenCentral()



dependencies 
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.flywaydb:flyway-core')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')

我已经在 MySql 中手动创建了 2 个数据库 数据库表快照 -

项目结构 -

例外

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywa
ydb.core.api.FlywayException: Unable to check whether schema `spring-flyway` is empty
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
        at com.boot.flyway.flywaydemo.FlywaydemoApplication.main(FlywaydemoApplication.java:10) [main/:na]
Caused by: org.flywaydb.core.api.FlywayException: Unable to check whether schema `spring-flyway` is empty
        at org.flywaydb.core.internal.dbsupport.Schema.empty(Schema.java:95) ~[flyway-core-3.2.1.jar:na]
        at org.flywaydb.core.Flyway$1.execute(Flyway.java:1021) ~[flyway-core-3.2.1.jar:na]
        at org.flywaydb.core.Flyway$1.execute(Flyway.java:1006) ~[flyway-core-3.2.1.jar:na]
        at org.flywaydb.core.Flyway.execute(Flyway.java:1418) ~[flyway-core-3.2.1.jar:na]
        at org.flywaydb.core.Flyway.migrate(Flyway.java:1006) ~[flyway-core-3.2.1.jar:na]
        at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
        ... 18 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'events' in information_schema
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_121]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_121]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_121]
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966) ~[mysql-connector-java-5.1.45.jar:5.1.45]
        at org.flywaydb.core.internal.dbsupport.JdbcTemplate.queryForInt(JdbcTemplate.java:156) ~[flyway-core-3.2.1.jar:na]
        at org.flywaydb.core.internal.dbsupport.mysql.MySQLSchema.doEmpty(MySQLSchema.java:49) ~[flyway-core-3.2.1.jar:na]
        at org.flywaydb.core.internal.dbsupport.Schema.empty(Schema.java:93) ~[flyway-core-3.2.1.jar:na]
        ... 25 common frames omitted

我无法解决此异常,请到处搜索。我是否缺少与 Flyway 相关的任何配置。

【问题讨论】:

你使用什么版本的 MySQL? @JánHalaša 版本为mysql-connector-java5.1.45,安装的mysql 是'5.0.45-community-nt',flyway-core 是3.2.1 【参考方案1】:

您使用的mysql-connector 比您的 MySQL 服务器更新,并且连接器希望它具有 MySQL 5.1 中引入的事件功能。这就是它找不到events 表的原因:

Unknown table 'events' in information_schema

您可能应该升级您的 MySQL 服务器(5.0 版非常旧)。正如 Axel Fontaine 在他的回答中提到的,Flyway 需要 MySQL 5.1 或更高版本。

【讨论】:

【参考方案2】:

作为documented on the Flyway website,Flyway 仅支持 MySQL 5.1 及更新版本。

【讨论】:

为啥flyway迁移需要JPA依赖才能运行

...【发布时间】:2017-09-1710:07:57【问题描述】:你好,我是SpringBootgradleProject的新手。我正在尝试使用SpringBoot应用程序运行Flyway迁移。这就是我在gradle中设置依赖项的方式,首先,我 查看详情

SpringBoot 的 DataJpaTest 与 Flyway

】SpringBoot的DataJpaTest与Flyway【英文标题】:SpringBoot\'sDataJpaTestwithFlyway【发布时间】:2018-01-0810:10:05【问题描述】:我在我的项目中使用flyway来设置数据库架构。当我尝试使用@DataJpaTest注释测试一些JPA层时,我在启动时收到此错... 查看详情

Spring Data JPA / Flyway 转义双引号

...:08:47【问题描述】:我有一个使用SpringDataJPA、Flyway和H2的SpringBoot项目,它正在尝试处理包含以下SQL语句的迁移脚本:ALTERROLEcurrent_userSETsearch_pathTO"$user",publi 查看详情

创建 jpa 表后的 Flyway 迁移

】创建jpa表后的Flyway迁移【英文标题】:Flywaymigrationafterjpatableshavebeencreated【发布时间】:2019-03-0307:39:28【问题描述】:我在springbootjpa项目中使用flyway数据库迁移工具。当我第一次运行服务器时,flyway想要迁移sql,但尚未创建表... 查看详情

Flyway 与 JPA + OSGi 的集成

】Flyway与JPA+OSGi的集成【英文标题】:IntegrationofFlywayintoJPA+OSGi【发布时间】:2018-11-0700:18:36【问题描述】:在我当前的项目中,我已将JPA集成到OSGi独立应用程序中。对于集成,我采用了以下OSGi规范实现:OSGiR7平台(Equinox3.13.0)JPA2... 查看详情

Oracle 中的 JPA 和 Flyway 布尔类型

】Oracle中的JPA和Flyway布尔类型【英文标题】:JPA&FlywaybooleantypeinOracle【发布时间】:2018-07-1717:03:33【问题描述】:我使用Flyway和SpringJPA来创建表和持久性。以下方法适用于H2嵌入式数据库(设置为oracle兼容模式)。但我注意到... 查看详情

在 flyway 迁移脚本之前执行 JPA 表创建

】在flyway迁移脚本之前执行JPA表创建【英文标题】:ExecuteJPAtablecreationbeforeflywaymigrationscript【发布时间】:2019-01-0906:41:11【问题描述】:我想使用这个Spring属性文件进行数据库配置。spring.jmx.enabled=falsespring.datasource.jndi-name=java:/glo... 查看详情

Flyway Spring JPA2 集成 - 可以保持模式验证吗?

】FlywaySpringJPA2集成-可以保持模式验证吗?【英文标题】:FlywaySpringJPA2integration-possibletokeepschemavalidation?【发布时间】:2016-08-2810:49:56【问题描述】:嗨,我有一个Web应用程序,我正在尝试集成JPA2(Hibernate)+Spring+Flyway我像这样在我... 查看详情

具有 JPA 依赖关系的 Flyway Spring Boot Autowired Bean

】具有JPA依赖关系的FlywaySpringBootAutowiredBean【英文标题】:FlywaySpringBootAutowiredBeanswithJPADependency【发布时间】:2018-06-2105:43:21【问题描述】:我正在使用Flyway5.0.5,但我无法创建具有自动装配属性的java(SpringJdbcMigration)...它们最终... 查看详情

springboot整合flyway

SpringBoot整合Flyway数据库版本迁移工具使用Flyway官网:https://flywaydb.org/documentation/使用感想:数据库的传统部署到自动化部署,工具的便利性使的开发更具效率性!简介:Flyway是一个开源数据库迁移工具。与配置相比,它极力主张... 查看详情

springboot整合flyway常见问题科普解决(代码片段)

很多网上关于Flyway整合springboot的说明并不明确Flyway在6.0之前是有兼容的,在6.0之后是没有做兼容的,小编的版本是6.0+报错如下:Causedby:org.flywaydb.core.api.FlywayException:Validatefailed:Detectedfailedmigrationtoversion2.3.0.2020081801(rolerig... 查看详情

springboot数据库管理-用flyway对数据库管理和迁移

上文介绍了Liquibase,以及和SpringBoot的集成。除了Liquibase之外,还有一个组件Flyway也是经常被使用到的类似的数据库版本管理中间件。本文主要介绍Flyway,以及SpringBoot集成Flyway。@pdai知识准备需要了解Flyway和要解决的问题,以及一... 查看详情

springboot集成flyway,数据库是oracle

一.springboot2.x,oracle11,用flyway5.x的报错,后来用了4.x的在dependencies加入了oracle和flyway的配置<dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2 查看详情

如何在 SpringBoot 应用程序迁移之前运行 flyway:clean?

】如何在SpringBoot应用程序迁移之前运行flyway:clean?【英文标题】:Howtorunflyway:cleanbeforemigrationsinaSpringBootapp?【发布时间】:2015-08-3001:54:17【问题描述】:我正在使用Springboot和Flyway。迁移工作得很好,但我希望能够在应用程序上... 查看详情

springboot整合flyway常见问题科普解决(代码片段)

很多网上关于Flyway整合springboot的说明并不明确Flyway在6.0之前是有兼容的,在6.0之后是没有做兼容的,小编的版本是6.0+报错如下:Causedby:org.flywaydb.core.api.FlywayException:Validatefailed:Detectedfailedmigrationtoversion2.3.0.2020 查看详情

Spring Boot Jpa 批处理-CannotCreateTransactionException

】SpringBootJpa批处理-CannotCreateTransactionException【英文标题】:SpringBootJpaBatch-CannotCreateTransactionException【发布时间】:2018-10-1420:32:21【问题描述】:我有一个SpringBoot+JPA+Spring批处理(+Spring集成)项目。还有Flyway,配置分开。我已将S... 查看详情

使用 Spring Boot 进行 Flyway 修复

】使用SpringBoot进行Flyway修复【英文标题】:FlywayrepairwithSpringBoot【发布时间】:2016-09-2413:23:33【问题描述】:我不太明白当在SpringBoot项目中使用Flyway迁移失败时我应该做什么。我只需在pom.xml中添加Flyway依赖项即可激活Flyway。一... 查看详情

springboot集成flyway实现数据库版本控制?

...家介绍一款比较好用的数据库版本控制工具Flyway。在通过SpringBoot构建微服务的过程中,一般情况下在拆分微服务的同时,也会按照系统功能的边界对其依存的数据库进行拆分。在这种情况下,微服务的数据库版本管理对于研发... 查看详情