Back to Flyway

MySQL

documentation/Reference/Database Driver Reference/MySQL.md

latest4.9 KB
Original Source
  • Verified Versions: 5.7, 8.0, 9.4
  • Maintainer: {% include redgate-badge.html %}

Supported Versions and Support Levels

{% include database-boilerplate.html %}

Drivers

-MySQLMariaDB
URL formatjdbc:mysql://host:port/databasejdbc:mysql://host:port/database
SSL supportYes - add ?useSsl=trueYes - add ?useSsl=true
Ships with Flyway Command-lineNo
Connector/J DownloadYes
Maven Central coordinatesmysql:mysql-connector-javaorg.mariadb.jdbc:mariadb-java-client
Supported versions5.1.44 and later2.0.0 and later
Default Java classcom.mysql.jdbc.Driverorg.mariadb.jdbc.Driver

Compatibility

  • If a MySQL driver is not present on the project classpath, MariaDB will be used as a fallback driver. If this is not desired, add disableMariaDbDriver to your database URL.
  • Since Flyway 10.7.0, the MariaDB driver included in the Flyway Command-line no longer accepts MySQL URLs by default. To continue using MySQL URLs with the MariaDB driver, add permitMysqlScheme=true to your database URL.

Java Usage

MySQL support is a separate dependency for Flyway and will need to be added to your Java project to access these features.

Maven

Redgate

xml
<dependency>
    <groupId>com.redgate.flyway</groupId>
    <artifactId>flyway-mysql</artifactId>
</dependency>

Open Source

xml
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-mysql</artifactId>
</dependency>

Gradle

Redgate

groovy
buildscript {
    dependencies {
        implementation "com.redgate.flyway:flyway-mysql"
    }
}

Open Source

groovy
buildscript {
    dependencies {
        implementation "org.flywaydb:flyway-mysql"
    }
}

SQL Script Syntax

  • Standard SQL syntax with a default statement delimiter of ; (semicolon)
  • If you are defining stored procedures and need to use semicolons in your script then Flyway will respect prefixing your script with the DELIMITER statement to change the statement delimiter
  • Comment directives generated by mysqldump /!.../;
  • MySQL-style single-line comments (# Comment)

Compatibility

  • DDL exported by mysqldump can be used unchanged in a Flyway migration.
  • Any MySQL SQL script executed by Flyway, can be executed by the MySQL command-line tool and other MySQL-compatible tools (after the placeholders have been replaced).

Example

sql
/* Single line comment */
CREATE TABLE test_data (
 value VARCHAR(25) NOT NULL,
 PRIMARY KEY(value)
);

/*
Multi-line
comment
*/

-- MySQL procedure
DELIMITER //
CREATE PROCEDURE AddData()
 BEGIN
   # MySQL-style single line comment
   INSERT INTO test_data (value) VALUES ('Hello');
 END //
DELIMITER;

CALL AddData();

-- MySQL comments directives generated by mysqlsump
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

-- Placeholder
INSERT INTO ${tableName} (name) VALUES ('Mr. T');

Authentication

Flyway supports the following MySQL authentication methods:

  • MySQL Option Files

Option Files

{% include teams.html %}

A username and password can be retrieved from MySQL option files for authentication, in which case they do not need to be supplied in configuration. The following table lists which option files are searched for per operating system, in order.

WindowsOther
%WINDIR%\my.ini, %WINDIR%\my.cnf/etc/my.cnf
C:\my.ini, C:\my.cnf/etc/mysql/my.cnf
%MYSQL_HOME%\my.ini, %MYSQL_HOME%\my.cnf$MYSQL_HOME/my.cnf
%APPDATA%\MySQL\.mylogin.cnf~/.my.cnf
N/A~/.mylogin.cnf

You can read more about MySQL option files here.

Limitations

  • No support for option file inclusions
  • No support for loading properties other than user and password from option files