Using Liquibase MongoDB Pro with MongoDB Community and Enterprise Server
MongoDB offers several NoSQL database products. This guide describes how to use Liquibase MongoDB Pro with MongoDB Community Server and MongoDB Enterprise Server. You need a Liquibase Pro license key to use it.
This guide does not imply support for third-party clones or emulations of MongoDB. For a tutorial on using Liquibase with Amazon DocumentDB, see Using Liquibase MongoDB Pro with Amazon DocumentDB. This guide also does not describe the community-maintained Liquibase MongoDB extension. For information on that extension, see Contribute Docs: Use Liquibase with MongoDB.
Verified database versions
- 7
- 6
- 5
For more information on Liquibase Pro and MongoDB version requirements, see Verified database versions.
Deprecated versions
MongoDB 4.4.x is deprecated as of February 29, 2024.
Prerequisites
- Introduction to Liquibase: Dive into Liquibase concepts.
- Install Liquibase: Download Liquibase on your machine.
- Ensure Java is installed: Liquibase requires Java to run. If you used the Liquibase Installer, Java is included automatically. Otherwise, you must install Java manually.
- Get Started with Liquibase: Learn how to use Liquibase with an example database.
- Design Your Liquibase Project: Create a new Liquibase project folder and organize your changelogs.
- How to Apply Your Liquibase Pro License Key: If you use Liquibase Pro, activate your license.
Configure MongoDB
- Ensure that your Configuring User Roles for MongoDB Pro are established before continuing.
- Download and Install mongosh if it is not already installed on your machine.
- Download Java 11. The MongoDB Pro extension requires it.
Note: mongosh
is mandatory to use MongoDB with Liquibase Pro and it must be accessible to Liquibase. We recommend that mongosh is in the system PATH
environment variable. If it is not, that location of mongosh must be manually specified in the liquibase.mongosh.conf
file.
Tip: Java 11 may already be present on your machine if you used the installer to install Liquibase. We recommend installing Liquibase with Java 11 with the installer asset available on GitHub.
Tip: TLS/SSL is not required to use Liquibase Pro and MongoDB. This is only required if you are using TLS/SSL because it is active on your MongoDB server or because you are using MongoDB Atlas.
To configure the TLS/SSL encrypted connection for the MongoDB Pro extension:
- Configure the MongoDB instance by first following Mongo's configuration guide.
- Verify that it works by following Mongo's verification guide.
- Add the
--tlsCertificateKeyFile
certificate that is produced in the configuration step above to the Java keystore. The--tlsCertificateKeyFile
specifies the.pem
file that contains mongosh's certificate. -
Specify your TLS connection information in the Liquibase
--url
argument, using the formatmongodb+srv://<hostname>/<database>
. You can pass this argument in CLI, liquibase.properties file, or set it as an environment variable:Example: liquibase.properties file:
liquibase.command.url=mongodb://localhost:27017/lbcat?tls=true&tlsCAFile=mongodb.pem
Environment variable:LIQUIBASE_COMMAND_URL=mongodb://localhost:27017/lbcat?tls=true&tlsCAFile=mongodb.pem
If you are using a Java keystore that is not the default, you must add the necessary environment variables before running Liquibase commands.
On Linux:
export JAVA_OPTS="-Djavax.net.ssl.keyStore=PATH_TO_KEYSTORE/cacerts -Djavax.net.ssl.keyStorePassword=PASSWORD"
On Windows:
set JAVA_OPTS=-Djavax.net.ssl.keyStore="PATH_TO_KEYSTORE\\cacerts" -Djavax.net.ssl.keyStorePassword=PASSWORD
Do not use
setx
as it addskeyStorePassword
to system environment variables.
On Linux:
sudo keytool -importcert -trustcacerts -file PATH_TO_CERT_FILE/mongodb-cert.crt -cacerts -storepass changeit -alias MongoDB
On Windows, open terminal in Administrator mode and run:
keytool -importcert -trustcacerts -file PATH_TO_CERT_FILE\\mongodb-cert.crt -cacerts -storepass changeit -alias mongodb
Note: The default password for keystore is changeit
.
Troubleshooting errors
After configuring TLS/SSL for Liquibase and MongoDB, you may come across an error that states:
Error: Could not find or load main class Files\\Java\\{jdk-version}.security.cacerts Caused by: java.lang.ClassNotFoundException: Files\\Java\\{jdk-version}.security.cacerts
This means that Liquibase is struggling to find the certification file. To resolve the issue, run the following command in the CLI:
On Windows:
set JAVA_OPTS=-Djavax.net.ssl.keyStore="%JAVA_HOME%\\lib\\security\\cacerts"
On Linux:
export JAVA_OPTS=-Djavax.net.ssl.keyStore=$JAVA_HOME/lib/security/cacerts
Install drivers
CLI users
If you currently have the Liquibase MongoDB open-source extension installed, remove it and the associated drivers from your machine.
To use Liquibase and MongoDB, you must download the JAR file that contains the Liquibase MongoDB Pro extension and the JDBC drivers.
liquibase/lib
directory.
Maven users
To use Liquibase with Maven, pom.xml
file. Using this information, Maven automatically downloads the driver JAR from Maven Central when you build your project.
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-commercial-mongodb</artifactId>
<version>1.3.0</version>
</dependency>
Configure connection
- Ensure your MongoDB database is configured. See Install MongoDB for more information.
-
Ensure your Liquibase Pro license key is specified. For example, in a
liquibase.properties
file (defaults file):licenseKey: zQl8kNZjZgSp9LvqWQFAtGwiHrpg97UyAfQrNSiJQBCDH8FQPdDzANUpIe4Bj3CZA2IXgDBaoYZFvMw0E/s4JcECB3/A6jO+...
- Specify the database URL in the
liquibase.properties
file (defaults file), along with other properties you want to set a default value for. Liquibase does not parse the URL. You can either specify the full database connection string or specify the URL using your database's standard connection format:
url: mongodb://hostname:27017/myDatabase
Note: If you are unsure about how to configure the url
property, refer to Connection String URI Format.
Test connection
-
Create a text file called
changelog
(.js
,.xml
,.yaml
, or.json
) in your project directory and add a changeset. - Formatted Mongo:
runWith:mongosh
- XML:
runWith="mongosh"
- YAML:
runWith: mongosh
- JSON:
"runWith": "mongosh"
- Navigate to your project folder in the CLI and run the Liquibase
status
command to see whether the connection is successful: - Then execute these changes to your database with the
update
command: - From a database UI tool, ensure that your database contains the
myCollection
object you added along with the DATABASECHANGELOG collection and DATABASECHANGELOGLOCK collection.
In each changeset, you must specify the mongosh
native executor using the runWith
attribute:
Alternatively, you can use native mongosh
scripts directly with Liquibase. The MongoDB Pro extension lets you use MongoDB's native language of MongoDB Query Language (MQL), which you may be storing in JavaScript files, in Liquibase changesets and Change Types. This is possible because MongoDB Shell (mongosh
) is compatible with Liquibase Pro. For more information, see MongoDB: Write Scripts. Example syntax:db.createCollection('customers');
You can use XML, YAML, or JSON "modeled" changelogs instead of writing plain or Formatted Mongo scripts in JavaScript files. XML, YAML, and JSON changelogs have all the functionality of a Formatted Mongo changelog, like specifying metadata tags and including other changelogs. You must specify mongosh
as the value of runWith
in each changeset.
In a modeled changelog, you can specify mongosh
(MongoDB Query Language) code within the mongo
and mongoFile
Change Types. Liquibase then implements this in your database. However, the actual MQL you specify is the same as in Formatted Mongo changelogs.
If you want to use the include
or includeAll
tag in a root changelog, it must be an XML, YAML, or JSON file. The child changelogs you reference can be written in Formatted Mongo, XML, YAML, or JSON.
With the Liquibase MongoDB Pro extension 1.3.0+, you can use a formatted Mongo changelog for MongoDB Pro, similar to a formatted SQL changelog. This lets you use mongosh
scripts written in MongoDB Query Language (MQL) directly in Liquibase.
All Liquibase formatted Mongo changelogs must use the file extension .JS
or .js
(JavaScript). They must also begin with a changelog header: // liquibase formatted mongodb
. Your changesets must each specify runWith:mongosh
. For example:
// liquibase formatted mongodb
// changeset your.name:1 labels:example-label context:example-context runWith:mongosh
// comment: example comment
db.createCollection('customers');
// rollback db = db.person.drop()
The Liquibase MongoDB Pro extension uses a unique mongodb-pro
XML namespace and XSD files in the changelog header. However, the ext
prefix used with other extensions is backwards-compatible:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongodb-pro="http://www.liquibase.org/xml/ns/pro-mongodb"
xmlns:mongodb="http://www.liquibase.org/xml/ns/mongodb"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/pro-mongodb http://www.liquibase.org/xml/ns/pro-mongodb/liquibase-pro-mongodb-latest.xsd
http://www.liquibase.org/xml/ns/mongodb http://www.liquibase.org/xml/ns/mongodb/liquibase-mongodb-latest.xsd">
<changeSet id="1" author="your.name" runWith="mongosh">
<mongodb-pro:mongo dbms="mongodb">
db.createCollection('person')
</mongodb-pro:mongo>
<rollback>
<mongodb-pro:mongo>
db.person.drop()
</mongodb-pro:mongo>
</rollback>
</changeSet>
<changeSet id="2" author="your.name" runWith="mongosh">
<mongodb-pro:mongoFile dbms="mongodb" path="scriptFile.txt" relativeToChangelogFile="true"/>
<rollback>
<mongodb-pro:mongo>
db.company.drop()
</mongodb-pro:mongo>
</rollback>
</changeSet>
</databaseChangeLog>
databaseChangeLog:
- changeSet:
id: 1
author: your.name
runWith: mongosh
changes:
- mongo:
mongo: "db.createCollection('person')"
dbms: mongodb
- rollback:
mongo:
mongo: "db.person.drop()"
- changeSet:
id: 2
author: your.name
runWith: mongosh
changes:
- mongoFile:
path: "scriptFile.txt"
dbms: mongodb
relativeToChangelogFile: "true"
- rollback:
mongo:
mongo: "db.company.drop()"
{
"databaseChangeLog": [
{
"changeSet": {
"id": "1",
"author": "your.name",
"runWith": "mongosh",
"changes": [
{
"mongo": {
"dbms": "mongodb",
"mongo": "db.createCollection('person')"
}
}
],
"rollback": [
{
"mongo": {
"mongo": "db.person.drop()"
}
}
]
}
},
{
"changeSet": {
"id": "2",
"author": "your.name",
"runWith": "mongosh",
"changes": [
{
"mongoFile": {
"dbms": "mongodb",
"path": "scriptFile.txt",
"relativeToChangelogFile": true
}
}
],
"rollback": [
{
"mongo": {
"mongo": "db.company.drop()"
}
}
]
}
}
]
}
Alternatively, you can specify some Change Type behavior using the MongoDB Liquibase Open Source syntax, which is included in your MongoDB Liquibase Pro installation. These Change Types run corresponding commands in MongoDB. This syntax is only valid for XML, YAML, and JSON changelogs.
For a list of all Pro and OSS MongoDB Change Types and full syntax examples, see Liquibase Change Types for MongoDB.
Note: You can specify arguments in the CLI or keep them in the Liquibase properties file.
If your connection is successful, you'll see a message like this:
4 changesets have not been applied to <your_connection_url>
Liquibase command 'status' was executed successfully.
liquibase update --changelog-file=<changelog.xml>
If your update
is successful, Liquibase runs each changeset and displays a summary message ending with:
Liquibase: Update has been successful.
Liquibase command 'update' was executed successfully.
Tip: You can use MongoDB Compass to easily view collections in your database. For example, run the commands use myDatabase
and db.myCollection.find()
.
Now you're ready to start making deployments with Liquibase!