写点什么

flywa 报错 Migration checksum mismatch for migration version 1.1 -> Applied to database : 1332862643 ->

作者:刘大猫
  • 2025-09-09
    黑龙江
  • 本文字数:4765 字

    阅读完需:约 16 分钟

详情错误日志


  .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::                (v2.6.3)
2022-11-01 14:50:33.363 INFO 26596 --- [ main] com.flyway.FlaywayApplication : Starting FlaywayApplication using Java 1.8.0_71 on HYT211145187-01 with PID 26596 (G:\WorkSpace\springboot-test\flyway-test\target\classes started by 211145187 in G:\WorkSpace\springboot-test)2022-11-01 14:50:33.366 INFO 26596 --- [ main] com.flyway.FlaywayApplication : No active profile set, falling back to default profiles: default2022-11-01 14:50:34.336 INFO 26596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8015 (http)2022-11-01 14:50:34.372 INFO 26596 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2022-11-01 14:50:34.373 INFO 26596 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]2022-11-01 14:50:34.460 INFO 26596 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2022-11-01 14:50:34.460 INFO 26596 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1052 ms2022-11-01 14:50:34.839 INFO 26596 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.5.7 by Redgate2022-11-01 14:50:34.844 INFO 26596 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariCP - Starting...2022-11-01 14:50:45.205 INFO 26596 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariCP - Start completed.2022-11-01 14:50:45.225 INFO 26596 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://10.110.13.86:3306/flyway (MySQL 5.7)2022-11-01 14:50:45.297 WARN 26596 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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 java.lang.reflect.InvocationTargetException2022-11-01 14:50:45.297 INFO 26596 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariCP - Shutdown initiated...2022-11-01 14:50:45.301 INFO 26596 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariCP - Shutdown completed.2022-11-01 14:50:45.305 INFO 26596 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]2022-11-01 14:50:45.314 INFO 26596 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.2022-11-01 14:50:45.343 ERROR 26596 --- [ main] o.s.boot.SpringApplication : Application run failed
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 java.lang.reflect.InvocationTargetException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3] at com.flyway.FlaywayApplication.main(FlaywayApplication.java:14) [classes/:na]Caused by: java.lang.reflect.InvocationTargetException: null at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_71] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_71] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_71] at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_71] at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:70) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15] ... 18 common frames omittedCaused by: org.flywaydb.core.api.FlywayException: Validate failed: Migration checksum mismatch for migration version 1.1-> Applied to database : 1332862643-> Resolved locally : -826751737
at org.flywaydb.core.Flyway.doValidate(Flyway.java:292) ~[flyway-core-6.5.7.jar:na] at org.flywaydb.core.Flyway.access$100(Flyway.java:73) ~[flyway-core-6.5.7.jar:na] at org.flywaydb.core.Flyway$1.execute(Flyway.java:166) ~[flyway-core-6.5.7.jar:na] at org.flywaydb.core.Flyway$1.execute(Flyway.java:158) ~[flyway-core-6.5.7.jar:na] at org.flywaydb.core.Flyway.execute(Flyway.java:527) ~[flyway-core-6.5.7.jar:na] at org.flywaydb.core.Flyway.migrate(Flyway.java:158) ~[flyway-core-6.5.7.jar:na] ... 25 common frames omitted

Process finished with exit code 1
复制代码


报错原因:修改了已经执行过的迁移文件,比如 V 开头的叫 V1.1__add_flyway_test_table.sql 这个名字的文件,重新执行这个更改的迁移文件时就会产生新的校验和“checkSum"与原来校验和不符合,导致报该错误,原则上不可以直接修改已经执行的迁移文件,要修改需要新建迁移文件进行修改。


举例说明:比如最开始我有个文件 V1.1__add_flyway_test_table.sql,里面有个 insert 插入一条数据的 sql,然后我启动整个项目,执行 sql 插入一条数据,且 V 开头的文件按常理已经执行过一次了,后面压根不会再次执行,但我手欠儿,再次对 V 开头的文件 V1.1__add_flyway_test_table.sql 进行修改或者新增几条 sql,然后再次启动项目就会报错误 5 的错误了。


解决方案


  • 1)方案1(不推荐):涉及的 2 张表 flyway_schema_history 和 flyway_test_table 都删除,这会造成已有的数据丢失、混乱。

  • 2)方案2(推荐):可以使用 Flyway Maven 插件,双击运行:flyway:repair ,修复该错误,再重新执行:flyway:migrate。这样项目就能再次启动。

  • 3)方案3(不推荐):删除 2 张表 flyway_schema_history 和 flyway_test_table,然后重新启动项目把所有 sql 脚本从头到尾执行一遍。缺点:万一数据量太大太耗时,甚至可能出错。


碰到个问题:原来的 V1.1__add_flyway_test_table.sql 里面有个内容,然后我对它修改后,执行 flyway:repair,在执行 flyway:migrate 后,再次启动项目发现 V1.1__add_flyway_test_table.sql 新增的内容没有执行?数据库没有产生新数据?


答案:应该是不会执行的,除非新增个 V 开头的迁移文件才会执行,即:对已经执行过的 V 开头的迁移文件修改后也不会执行,需要新建迁移文件进行修改才会生效。


详情请看我的博客:↓


springboot使用flyway,使用介绍、个人总结及报错场景如何修改

用户头像

刘大猫

关注

还未添加个人签名 2022-08-23 加入

还未添加个人简介

评论

发布
暂无评论
flywa报错Migration checksum mismatch for migration version 1.1 -> Applied to database : 1332862643 ->_人工智能_刘大猫_InfoQ写作社区