Skip to main content

MySQL Overview

The MYSQL connector stores MQTT messages in MySQL databases. It supports SSL connections, parameterized queries, and automatic reconnection for reliable data persistence.
MySQL is ideal for web applications and general-purpose data storage. Its wide ecosystem support makes it a reliable choice for most IoT data persistence needs.

Basic Syntax


Connection Configuration

SQL_CONFIG Parameters

string
required
MySQL server hostname or IP address.
string
required
Target database name.
string
required
Database username.
string
required
Database password.
integer
MySQL port. Default: 3306.
boolean
Enable SSL connection. Default: false.
integer
Connection timeout in seconds. Default: 30.
integer
Maximum connections in the pool. Passed to the MySQL driver. Omit to keep the driver default.
integer
Minimum connections in the pool. Passed to the MySQL driver. Omit to keep the driver default. Must be less than or equal to MAX_POOL_SIZE.
integer
How long an idle pooled connection may be kept. Passed to the MySQL driver. Omit to keep the driver default.
integer
How long a pooled connection may be kept. Passed to the MySQL driver. Omit to keep the driver default.
integer
How many MQTT-triggered SQL events may run at once. Alias: MAX_CONCURRENT_WRITES. Optional.
string
default:"block"
What to do when MQTT-triggered writes are already at WRITE_CONCURRENCY. block waits, and WRITE_QUEUE_CAPACITY is ignored. drop_oldest and drop_newest apply WRITE_QUEUE_CAPACITY. Dropping an event logs a Warning and counts the drop.
integer
How many waiting MQTT-triggered events to keep when the policy is drop_oldest or drop_newest. Ignored when the policy is block.
Invalid pool values are refused. MIN_POOL_SIZE must be less than or equal to MAX_POOL_SIZE. A failed query does not mark the connector Disconnected. A constraint violation, a cast error, MySQL unknown-command (1047), an exhausted pool, and any other query failure leave it connected. Only a transport or connection failure marks it Disconnected, so one bad row does not stop later telemetry.
Never hardcode credentials in production. Use environment variables and encrypted secrets to keep sensitive values out of your connector definitions:
See Environment Variables & Secrets for setup and usage.

Reconnecting after a dropped connection

When the database ends the connector’s sessions or the connection drops, the connector is marked Disconnected. The next event reconnects it, straight away and then at most every 5 seconds until the database answers, and runs once the connection is back. No redeploy is needed. The event that was running on the dropped session fails and is not retried.

Text, names, and dates

  • The connection uses utf8mb4, and tables that STORE IN creates use utf8mb4 even when the database defaults to latin1, so emoji and CJK text are stored as sent.
  • STORE IN quotes table and column names, so a model field can be called key, order, or group.
  • A zero date (0000-00-00) reads back as 0001-01-01T00:00:00 instead of failing the query.
  • A query that runs longer than COMMAND_TIMEOUT fails at the timeout.

Agent queries

An agent can read this database when the connector opts in and the agent grants connector_query. The agent runs one read statement. It cannot insert, update, or delete. See Database queries. If you omit the agent login, MySQL uses the connector user inside a read-only transaction and rolls it back. The broker logs a warning. Give the agent its own read-only user. The person who asks and the agent’s DEFAULT_USER both need PublishSys on $SYS/Coreflux/Routes/<connector>/Query.

Event Configuration

EVENT Parameters

string
required
MQTT topic pattern that triggers the INSERT. Supports wildcards.
string
required
SQL INSERT statement with placeholders for dynamic values.
string
MQTT topic to publish query result status.

Query Placeholders


How placeholders are quoted

The broker places each placeholder in an event QUERY by the quotes around it. A publisher on the source topic can no longer change the SQL by putting SQL in the payload.
Action: Put every placeholder that carries text in single quotes (WHERE status = '{payload}'). Put a placeholder that names a table or column in backticks (FROM `{topic.2}`). An unquoted placeholder must be a number, a comma-separated list of numbers, true, false, or null. Anything else, including a bare name such as line_1, refuses the event. The message names the placeholder.
{env.NAME} and {secret.NAME} are placed as written. A value that itself contains {topic} is stored as text and is not expanded again. The same rules on the other SQL engines are in How placeholders are quoted. MongoDB and OpenSearch JSON queries place each value as one JSON value instead (see OpenSearch placeholders).

Writing Data

Store sensor readings with timestamps:
{value.json.value} stays unquoted because the reading is a number. If that field is text, put it in single quotes ('{value.json.value}'), or the event is refused.
Alternative: STORE IN with Models — Instead of writing EVENT queries, you can bind a model directly to this connector. Every PUBLISH MODEL call automatically inserts a row — no query needed:
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:
To trigger this query, publish the sensor ID to the source topic:
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:

Table Schema Examples


Troubleshooting

  • Verify SERVER and PORT are correct
  • Check MySQL is running and accepting connections
  • Verify firewall allows connections on port 3306
  • Check bind-address in MySQL configuration
  • Verify USERNAME and PASSWORD are correct
  • Check user has permissions on the DATABASE
  • Verify user can connect from your host
  • Check placeholder names match topic structure
  • Put text placeholders in single quotes, and table or column placeholders in backticks. An unquoted text value refuses the event. See How placeholders are quoted
  • Verify table and column names exist
  • Test query manually in MySQL client first

Next Steps

Data Storage Connectors Overview

Compare all storage options.

MariaDB Connector

Configure MariaDB storage (MySQL-compatible).
Last modified on December 4, 2026