Using Liquibase with MySQL
To test your connection, try running Liquibase with the JDBC driver located in the same directory as Liquibase:
liquibase --driver=com.mysql.cj.jdbc.Driver --classpath=mysql-connector-java-5.1.21-bin.jar --url="jdbc:mysql://<IP OR HOSTNAME>:<PORT>/<SCHEMA NAME>?autoReconnect=true&useSSL=FALSE|TRUE" --changeLogFile=dbchangelog-1.0.xml --username=<MYSQL USERNAME> --password=<MYSQL PASSWORD>generateChangeLog
Note: Formatted SQL changelogs generated by using Liquibase versions previous to 4.2 might cause issues because of the lack of space after a double dash ( -- ).
Tip: To fix those issues, add a space after the double dash. For example:
-- liquibase formatted sql instead of
--liquibase formatted sql and
-- changeset myname:create-table instead of
Creating New Liquibase projects with MySQL – Windows
The purpose of this document is to guide you through the process of creating a new Liquibase project with MySQL on a Windows machine. In this tutorial, you will generate an example project and follow the instructions to apply and learn concepts associated with creating new Liquibase projects with MySQL.
- If you have not installed the latest version of Liquibase, go to https://www.liquibase.org/download to install it.
- Ensure the
liquibase.batfile's path is set to a location in the PATH System variable.
- Go to https://dev.mysql.com/downloads/connector/j/ and download the jdbc driver jar file for MySQL.
Note: Place the jdbc jar driver file in a known directory so you can locate it easily.
To create a Liquibase project with MySQL on your Windows machine, begin with the following steps:
- Create a new project folder and name it
- In your
LiquibaseMySQLfolder, create a new text file and name it
dbchangelog.xml. changelog files contain a sequence of changesets, each of which make small changes to the structure of your database. To add this change:
- Open the
dbchangelog.xmlfile and update the changelog file with the following code snippet:
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"> </databaseChangeLog>
- In your
LiquibaseMySQLfolder, create another text file and name it
- Edit the
liquibase.propertiesfile to add the following properties:
changeLogFile: ../dbchangelog.xml url: jdbc:mysql://localhost:3306/my_schema username: root password: password driver: org.gjt.mm.mysql.Driver classpath: ../../Liquibase_Drivers/mysql-connector-java-5.1.48.jar
As you are creating this project on Windows OS, you must specify the path with double slashes in the
changeLogFile property. Also, use a relative path from your project directory to the driver jdbc jar file location in the
Note: If you already have a Liquibase Pro key and want to apply it to your project, add the following property to your liquibase.properties file.
liquibaseProLicenseKey: <paste license key>
- Add a changeset to the changelog. changesets are uniquely identified by
idattributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end. In the
dbchangelog.xmlfile, add a new
departmentcreate table changeset as follows:
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"> <changeSet id="1" author="bob"> <createTable tableName="department"> <column name="id" type="int"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(50)"> <constraints nullable="false"/> </column> <column name="active" type="boolean" defaultValueBoolean="true"/> </createTable> </changeSet> </databaseChangeLog>
Note: This changeset is an XML format. The corresponding SQL statement should look like the following:
CREATE TABLE "department" ("id" number (*,0), "name" VARCHAR2 (50 BYTE), "active" NUMBER (1, 0) DEFAULT 1 );
Open the command prompt and navigate to the
Run the following command:
From a database UI Tool (for example, MySQL Workbench), check your database changes under
my_schema. You should see a new
departmenttable added to the database. For example:
SELECT * FROM my_schema.department;
Also, you should see two more tables:
- DATABASECHANGELOG tracking table – This table keeps a record of all the changesets that were deployed. This way, next time when you deploy again, the changesets in the changelog will be compared with the DATABASECHANGELOG tracking table and only the new changesets that were not found in the DATABASECHANGELOG will be deployed. You will notice that a new row was created in that table with the changeset information we have just deployed. For this example:
- DATABASECHANGELOGLOCK – This table is used internally by Liquibase to manage access to the changelog table during deployment.