Skip to main content

MariaDB Overview

The MARIADB route stores MQTT messages in MariaDB databases. MariaDB is a MySQL-compatible database with enhanced features and performance optimizations.
MariaDB offers drop-in MySQL compatibility with additional features like improved replication and storage engines. Use the same configuration as MySQL with the MARIADB route type.

Basic Syntax

DEFINE ROUTE SensorDB WITH TYPE MARIADB
    ADD SQL_CONFIG
        WITH SERVER "mariadb.example.com"
        WITH PORT '3306'
        WITH DATABASE "iot_data"
        WITH USERNAME "iot_user"
        WITH PASSWORD "secure_password"
    ADD EVENT StoreSensorReading
        WITH SOURCE_TOPIC "sensors/+/reading"
        WITH QUERY "INSERT INTO readings (ts, sensor_id, value) VALUES (NOW(), '{sensor_id}', '{value.json}')"

Connection Configuration

SQL_CONFIG Parameters

SERVER
string
required
MariaDB server hostname or IP address.
DATABASE
string
required
Target database name.
USERNAME
string
required
Database username.
PASSWORD
string
required
Database password.
PORT
integer
MariaDB port. Default: 3306.
USE_SSL
boolean
Enable SSL connection. Default: false.
Never hardcode credentials in production. Use environment variables and encrypted secrets to keep sensitive values out of your route definitions:
WITH SERVER GET ENV "DB_HOST"
WITH USERNAME GET ENV "DB_USER"
WITH PASSWORD GET SECRET "DB_PASSWORD"
See Environment Variables & Secrets for setup and usage.

Writing Data

Store sensor data in MariaDB:
DEFINE ROUTE SensorStorage WITH TYPE MARIADB
    ADD SQL_CONFIG
        WITH SERVER "mariadb.example.com"
        WITH PORT '3306'
        WITH DATABASE "iot_data"
        WITH USERNAME "iot_user"
        WITH PASSWORD "secure_password"
    ADD EVENT StoreReading
        WITH SOURCE_TOPIC "sensors/#"
        WITH QUERY "INSERT INTO sensor_data (timestamp, topic, payload) VALUES (NOW(), '{source_topic}', '{value.json}')"
Alternative: STORE IN with Models — Instead of writing EVENT queries, you can bind a model directly to this route. Every PUBLISH MODEL call automatically inserts a row — no query needed:
DEFINE MODEL SensorReading
    ADD STRING "sensor_id"
    ADD DOUBLE "value"
    STORE IN "SensorStorage"
        WITH TABLE "sensor_data"
See Data Storage Overview for the full STORE IN workflow.

Reading Data

EVENTs also support SELECT queries to read data back from the database. Publish a message to the event’s SOURCE_TOPIC, and the query result is published to DESTINATION_TOPIC:
DEFINE ROUTE SensorDB WITH TYPE MARIADB
    ADD SQL_CONFIG
        WITH SERVER "mariadb.example.com"
        WITH PORT '3306'
        WITH DATABASE "iot_data"
        WITH USERNAME "iot_user"
        WITH PASSWORD "secure_password"
    ADD EVENT GetLatestReading
        WITH SOURCE_TOPIC "db/query/latest"
        WITH DESTINATION_TOPIC "db/result/latest"
        WITH QUERY "SELECT sensor_id, value FROM sensor_data WHERE sensor_id = '{payload}' ORDER BY timestamp DESC LIMIT 1"
To trigger this query, publish the sensor ID to the source topic:
Topic:   db/query/latest
Payload: temp001
The query result is published to db/result/latest, where your actions or external clients can consume it.

Timed Polling

EVENTs can also run SELECT queries on a fixed schedule using WITH EVERY:
DEFINE ROUTE DeviceRegistry WITH TYPE MARIADB
    ADD SQL_CONFIG
        WITH SERVER "mariadb.example.com"
        WITH PORT '3306'
        WITH DATABASE "iot_data"
        WITH USERNAME "iot_user"
        WITH PASSWORD "secure_password"
    ADD EVENT PollActiveDevices
        WITH EVERY 10 SECONDS
        WITH DESTINATION_TOPIC "dashboard/devices/active"
        WITH QUERY "SELECT device_id, name, current_status, last_seen FROM devices WHERE active = 1 ORDER BY name"

Troubleshooting

  • Verify SERVER and PORT are correct
  • Check MariaDB is running and accepting connections
  • Verify firewall allows connections on port 3306
  • Verify USERNAME and PASSWORD are correct
  • Check user has permissions on the DATABASE
  • Verify user can connect from your host

Next Steps

Data Storage Routes Overview

Compare all storage options.

SQL Server Route

Configure Microsoft SQL Server storage.