Flywaydb集成 [英] Flywaydb integration

查看:217
本文介绍了Flywaydb集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Flyway迁移集成到我的项目(portlet-Maven构建)中,但是我无法弄清楚如何执行所有必要的步骤-请您看看我到目前为止所做的事情并提出建议吗?失踪了吗?我阅读了文档和一些示例,但是没有完整的教程如何从头开始进行设置:

I am trying to integrate Flyway migration into my project (portlet - maven build) but I cannot figure out how to do all the necessary steps - could you please take a look on what I have done so far and advice what am I missing, please? I read the documentation and some examples but there is no complete tutorial how to set it up from scratch:

这是我所做的:

1)在pon.xml中添加了依赖项

1) Added dependencies in pon.xml

<dependency>
    <groupId>com.googlecode.flyway</groupId>
    <artifactId>flyway-core</artifactId>
    <version>2.3</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.21</version>
</dependency>

2)在src/main/resources/db/migration中添加了SQL脚本V1_Create_table_messages.sql

2) Added sql script V1_Create_table_messages.sql in src/main/resources/db/migration

CREATE TABLE messages (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    msgid VARCHAR(64) UNIQUE,
    sender VARCHAR(255),
    receiver VARCHAR(255),
);

3)使用以下方法创建了迁移类:

3) Created migration class with this:

 public void migrate() {
        try {       
            Flyway flyway = new Flyway();

            flyway.setDataSource("jdbc:mysql://localhost:3306/myDb", "user", "password");

            flyway.setInitOnMigrate(true);
            flyway.migrate();
        } catch (Exception e) {
            System.out.println("Error while migrate database");
        }
    }

当我调用此方法时,得到的是:(编辑!)

When I call this method, I get this: (EDITed!)

com.googlecode.flyway.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
Caused by: java.sql.SQLException: Invalid authorization specification message from server: "Access denied for user 'nobody'@'localhost' (using password: NO)"

setDataSource中的所有信息均已正确设置.我知道它可能有些愚蠢,但是我觉得我在这里设置Flyway的过程中缺少了重要的一步...

All the information in setDataSource are set correctly. I know its probably something dumb, but I feel I am missing some important step in setting up Flyway here...

感谢任何提示!

在pom.xml中添加了MySQL连接器依赖性

Added MySQL connector dependency in pom.xml

已解决:

问题是缺少mysql依赖项和URL中的额外斜线...

the problem was missing mysql dependency and extra slash in url...

推荐答案

检查您的migrate()方法.您设置了两次数据源.

Check your migrate() method. You set the data source twice.

flyway.setDataSource("jdbc:mysql://localhost:3306/myDb", "user", "password");

flyway.setDataSource(dataSource);

我猜想第二个setDataSource()会覆盖正确的第一个数据源.

I would guess that the second setDataSource() overwrites the correct first data source.

编辑

使用MySQL Java Connector而不是H2的正确POM定义为:

The correct POM definition to use the MySQL Java Connector instead of H2 would be:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.30</version>
</dependency>

这篇关于Flywaydb集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆