HiveMQ Enterprise Extension for MySQL

MySQL is an open-source relational database management system that is widely used to store and manage structured data.

Features

Requirements

If you do not provide a valid license, HiveMQ automatically uses a free trial license. Trial licenses for HiveMQ Enterprise Extensions are valid for 5 hours. For more license information or to request an extended evaluation license, contact HiveMQ sales.

Installation

  1. Place your HiveMQ Enterprise Extension for MySQL license file (.elic) in the license folder of your HiveMQ installation. (Skip this step if you are using a trial version of the extension).

    └─ <HiveMQ folder>
        ├─ bin
        ├─ conf
        ├─ data
        ├─ extensions
        │   ├─ hivemq-mysql-extension
        │   └─ ...
        ├─ license
        ├─ log
        └─ ...
  2. Before you enable the extension, you need to configure the extension to match your individual MySQL setup.
    For your convenience, we provide an example configuration conf/examples/config.xml that you can copy and modify as desired.
    The included config.xsd file outlines the schema and elements that can be used in the XML configuration.
    Your completed configuration file must be named config.xml and located in HIVEMQ_HOME/extensions/hivemq-mysql-extension/conf/config.xml.
    For detailed information on configuration options, see Configuration.

  3. To enable the HiveMQ Enterprise Extension for MySQL, locate the hivemq-mysql-extension folder in the extensions directory of your HiveMQ installation and remove the DISABLED file (if present).

To function properly, the HiveMQ Enterprise Extension for MySQL must be installed on all HiveMQ broker nodes in your HiveMQ cluster and the configuration file on each node must be identical.

Configuration

The extension configuration is divided into two sections:

  • MySQLs: Provides information about the MySQL databases to which your HiveMQ broker connects.

  • MQTT to MySQL Routes: Defines how MQTT messages are sent from your HiveMQ broker to the configured MySQL databases.

Extension Configuration File

The config.xml file for your MySQL extension must be located in the hivemq-mysql-extension/conf/ folder within the extensions folder of your HiveMQ installation.

The extension uses a simple but powerful XML-based configuration.

The conf/examples/config.xml file is a configuration example that has all the parameters you need to send MQTT messages from your HiveMQ MQTT broker to MySQL.

If you copy and reuse the conf/examples/config.xml file, be sure to move the file to /conf/config.xml before you enable your extension. For more information, see Installation.
Example MySQL database connection configuration
<hivemq-mysql-extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:noNamespaceSchemaLocation="config.xsd">
    <mysqls>
        <mysql>
            <id>my-mysql-id</id>
            <host>my.host.com</host>
            <port>3306</port>
            <database>my-database</database>
            <username>my-user</username>
            <password>my-password</password>
        </mysql>
    </mysqls>

    <mqtt-to-mysql-routes>
        <mqtt-to-mysql-route>
            <id>my-mqtt-to-mysql-route-insert</id>
            <mysql-id>my-mysql-id</mysql-id>
            <mqtt-topic-filters>
                <mqtt-topic-filter>#</mqtt-topic-filter>
            </mqtt-topic-filters>
            <processor>
                <insert-statement>
                    <table>mqtt_to_mysql_table</table>
                    <batching>true</batching>
                    <columns>
                        <column>
                            <name>topic</name>
                            <value>mqtt-topic</value>
                        </column>
                        <column>
                            <name>payload_utf8</name>
                            <value>mqtt-payload-utf8</value>
                        </column>
                    </columns>
                </insert-statement>
            </processor>
        </mqtt-to-mysql-route>

        <mqtt-to-mysql-route>
            <id>my-mqtt-to-mysql-route-template</id>
            <mysql-id>my-mysql-id</mysql-id>
            <mqtt-topic-filters>
                <mqtt-topic-filter>#</mqtt-topic-filter>
            </mqtt-topic-filters>
            <processor>
                <statement-template>path/to/my/statement-template.sql</statement-template>
            </processor>
        </mqtt-to-mysql-route>
    </mqtt-to-mysql-routes>
</hivemq-mysql-extension>

MySQL Databases Configuration

The <mysqls> section of your configuration lists the MySQL databases to which configured MQTT messages are routed.

You can define as many <mysql> tags as your use case requires.

Example MySQL database connection configuration
<mysqls>
    <mysql>
        <id>my-mysql-id</id>
        <host>my.host.com</host>
        <port>3306</port>
        <database>my-database</database>
        <username>my-user</username>
        <password>my-password</password>
    </mysql>
</mysqls>
Example MySQL database connection with a TLS configuration
<mysqls>
    <mysql>
        <id>my-mysql-id</id>
        <host>my.host.com</host>
        <port>3306</port>
        <database>my-database</database>
        <username>my-user</username>
        <password>my-password</password>
        <tls>
            <root-certificate>/path/to/root-certificate.pem</root-certificate>
            <client-certificate>/path/to/client-certificate.pem</client-certificate>
            <client-private-key>/path/to/client-private-key.pem</client-private-key>
        </tls>
    </mysql>
</mysqls>
You can use Environment variables to map properties such as the MySQL password.
Table 1. MySQL configuration parameters
Parameter Required Type Description

id

ID

The unique identifier of the MySQL connection. This string can only contain lowercase alphanumeric characters, dashes, and underscores.

host

String

The host name of the MySQL database.

port

Integer

The port of the MySQL database.

database

String

The name of the MySQL database.

username

String

The username to connect with basic authentication.

password

String

The password to connect with basic authentication.

tls

-

Complex

Optional TLS configuration to establish a secure connection to MySQL.

  • enabled: Optional setting that defines whether the TLS configuration is used to encrypt communication between the extension and MySQL. The default setting is true. To disable your TLS configuration, set the enabled parameter to false.

  • root-certificate: Path to a PEM encoded X.509 trusted root certificates. The internal JDK CA-certificates are used if this setting is not provided.

  • client-certificate: Path to a PEM encoded X.509 client certificate chain.

  • client-private-key: Path to a PKCS#8 private key file in PEM format.

  • verify-hostname: Optional setting that defines whether the extension checks the identity of the remote server. The default setting is true. To skip the verification, set the value to false.

MQTT to MySQL Routes

The <mqtt-to-mysql-routes> section of your extension configuration defines how MQTT messages are sent from the HiveMQ broker to MySQL.

You can define as many <mqtt-to-mysql-route> tags as your use case requires.

Example MQTT to MySQL route with an insert statement configuration
<mqtt-to-mysql-route>
    <id>my-mqtt-to-mysql-route-insert</id>
    <mysql-id>my-mysql-id</mysql-id>
    <mqtt-topic-filters>
        <mqtt-topic-filter>#</mqtt-topic-filter>
    </mqtt-topic-filters>
    <processor>
        <insert-statement>
            <table>mqtt_to_mysql_table</table>
            <batching>true</batching>
            <columns>
                <column>
                    <name>topic</name>
                    <value>mqtt-topic</value>
                </column>
                <column>
                    <name>payload_utf8</name>
                    <value>mqtt-payload-utf8</value>
                </column>
            </columns>
        </insert-statement>
    </processor>
</mqtt-to-mysql-route>
Example MQTT to MySQL route with a statement template configuration
<mqtt-to-mysql-route>
    <id>my-mqtt-to-mysql-route-template</id>
    <mysql-id>my-mysql-id</mysql-id>
    <mqtt-topic-filters>
        <mqtt-topic-filter>#</mqtt-topic-filter>
    </mqtt-topic-filters>
    <processor>
        <statement-template>path/to/my/statement-template.sql</statement-template>
    </processor>
</mqtt-to-mysql-route>
Table 2. MySQL parameters
Parameter Required Type Description

id

ID

The unique identifier of the mqtt-to-mysql-route. This string can only contain lowercase alphanumeric characters, dashes, and underscores.

mysql-id

IDREF

The ID of the MySQL database.

enabled

-

Boolean

Optional setting that defines whether the selected mqtt-to-mysql-route is enabled or disabled. The default setting is true. To disable the route, set to false.

mqtt-topic-filters

Complex

A list of one or more MQTT topic filters.

  • mqtt-topic-filter: The source MQTT topic filters to select which MQTT messages are routed to the MySQL database. You can define as many individual <mqtt-topic-filter> tags as your use case requires.

processor

Complex

Defines how HiveMQ converts MQTT messages into MySQL rows.

Two processor types are supported:

  • statement-template: The path to an SQL statement template file with a valid MySQL query. The path can be an absolute path or a relative path to the home folder of your HiveMQ extension for MySQL.

  • insert-statement: Use the XML configuration to define how to insert rows into MySQL. The extension creates the query.

    • table: The name of the table where rows are inserted.

    • batching: Optional setting to enable or disable batching. The default setting is true. To disable batching, set to false.

    • columns: A list of the table columns where values are inserted.

      • column: A column name and value binding.

        • name: The name of the column where the value is inserted.

        • value: The MQTT property placeholder to replace and bind to the selected column.

Insert Statements

Insert statements help you convert MQTT messages into MySQL rows, without the need to write your own SQL.

The insert statements provide the table name, target columns, and respective value placeholders. The extension fills the placeholders with MQTT PUBLISH properties and creates the insert query.

Batching is enabled by default for insert statements. To disable batching, set the <batching> tag in your <insert-statement> to false.
Example insert statement
<insert-statement>
    <table>mqtt_to_mysql_table</table>
    <batching>true</batching>
    <columns>
        <column>
            <name>topic</name>
            <value>mqtt-topic</value>
        </column>
        <column>
            <name>payload_utf8</name>
            <value>mqtt-payload-utf8</value>
        </column>
        <column>
            <name>my_user_property</name>
            <value>mqtt-user-properties$myUserPropertyName</value>
        </column>
    </columns>
</insert-statement>

Statement Templates

Statement templates are SQL files that help you convert MQTT messages into MySQL rows. These templates allow you to define placeholders that are later filled with the properties of the MQTT PUBLISH.

Placeholders start with ${ and end with }.

Example statement template
INSERT INTO mqtt_to_mysql_table(topic, payload_utf8, qos, retain, packet_id, payload_format_indicator, response_topic,
                                correlation_data_utf8, my_user_property, arrival_timestamp)
VALUES (${mqtt-topic},
        ${mqtt-payload-utf8},
        ${mqtt-qos},
        ${mqtt-retain},
        ${mqtt-packet-id},
        ${mqtt-payload-format-indicator},
        ${mqtt-response-topic},
        ${mqtt-correlation-data-utf8},
        ${mqtt-user-properties$myUserPropertyName},
        ${timestamp-iso-8601});
The HiveMQ extension automatically converts the template to a prepared statement and fills in all the placeholders.
Your HiveMQ extension automatically checks for the presence of unknown placeholders in your statement templates and insert statements. If an unknown placeholder is present, HiveMQ logs an error and the HiveMQ Extension for MySQL does not start.

The following table lists all placeholders the HiveMQ MySQL extension recognizes:

Table 3. Available placeholders
Name Type MySQL recommend data type Description

mqtt-topic

String

TEXT

The topic of the MQTT PUBLISH.

mqtt-payload-utf8

String

TEXT

The payload of the MQTT PUBLISH as a UTF-8 string.

mqtt-payload-base64

String

TEXT

The payload of the MQTT PUBLISH as a Base64 string.

mqtt-qos

String

TEXT

The QoS level of the MQTT PUBLISH (AT_MOST_ONCE, AT_LEAST_ONCE, EXACTLY_ONCE).

mqtt-retain

boolean

BOOLEAN

The retain flag of the MQTT PUBLISH.

mqtt-packet-id

int

INTEGER

The packet ID of the MQTT PUBLISH.

mqtt-payload-format-indicator

String

TEXT

The payload format indicator of the MQTT PUBLISH.

mqtt-response-topic

String

TEXT

The response topic of the MQTT PUBLISH.

mqtt-correlation-data-utf8

String

TEXT

The correlation data of the MQTT PUBLISH as a UTF-8 string.

mqtt-correlation-data-base64

String

TEXT

The correlation data of the MQTT PUBLISH as a Base64 string.

mqtt-user-properties-json

List

JSON

The user properties of the MQTT PUBLISH as a JSON array string.

mqtt-user-properties$property-name

String

TEXT

The value of the user property of the MQTT PUBLISH with the matching property name.

Example: mqtt-user-properties$myUserPropertyName will bind the value of the first user property with the name myUserPropertyName.

timestamp-ms

long

BIGINT

The arrival timestamp of the PUBLISH message represented as a UNIX timestamp value in milliseconds.

  • The timestamp of an incoming PUBLISH message records the moment the message arrived at the broker.

  • The timestamp of a message created via the Extension SDK records the moment the message passed to the PublishService.

  • The timestamp of a Will Publish message sent to subscribers records the moment the delivery of the message started.

timestamp-iso-8601

Instant

TIMESTAMP(3)

The same information as timestamp-ms represented as an ISO 8601 string.

Some properties in an MQTT PUBLISH message are optional. The number of placeholders the MySQL extension fills varies based on the properties that are present in the MQTT PUBLISH message.

Environment variables

HiveMQ offers placeholders that can be replaced with the content of environment variables when the configuration file is read. For many use cases, it can be beneficial or necessary to use environment variables to configure items such as ports and bind addresses on the system on which you run HiveMQ. For example, when you run HiveMQ in a containerized environment.

You can use ${YOUR_ENVVAR_NAME} in the config.xml file. HiveMQ replaces the placeholder with the value of the specified environment variable during startup.

Example to set an environment variable
export MY_MYSQL_PASSWORD=my-password
Example use of the environment variable in the configuration
<hivemq-mysql-extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:noNamespaceSchemaLocation="config.xsd">
    <mysqls>
        <mysql>
            <id>my-mysql-id</id>
            <host>my.host.com</host>
            <port>3306</port>
            <database>my-database</database>
            <username>my-user</username>
            <password>${MY_MYSQL_PASSWORD}</password>
        </mysql>
    </mysqls>
</hivemq-mysql-extension>
Result of the example configuration in HiveMQ
<hivemq-mysql-extension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:noNamespaceSchemaLocation="config.xsd">
    <mysqls>
        <mysql>
            <id>my-mysql-id</id>
            <host>my.host.com</host>
            <port>3306</port>
            <database>my-database</database>
            <username>my-user</username>
            <password>my-password</password>
        </mysql>
    </mysqls>
</hivemq-mysql-extension>
Make sure that HiveMQ is started in the same context as your environment variables are set, otherwise HiveMQ will not be able to access them.