Flywaydb integration

2019-08-16 23:34发布

问题:

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:

Here is what I have done:

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) 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) 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)"

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...

Thanks for any tips!

EDIT2: Added MySQL connector dependency in pom.xml

SOLVED:

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

回答1:

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

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

and

flyway.setDataSource(dataSource);

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

EDIT

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>