MySQL
The features or components discussed in this document are available in TDengine TSDB-Enterprise only. TDengine TSDB-OSS does not include these features or components.
TDengine can continuously publish the results of a subscription query to a single MySQL table. taosX creates a TMQ query topic from the SQL, writes batches to the target table by primary key, and commits the consumer position only after each write succeeds.
Prepare the Environment
- Verify that
taosdandtaosAdapterare running and that taosX is installed with data publishing support. - Use MySQL 5.6, 5.7, or 8.0 and later. The target database must already exist. taosX does not create databases.
- Set
WAL_RETENTION_PERIODon the source database to a value greater than zero. The source user needs permission to create topics and subscribe. The target user needs permission to write to the table and, if automatic table creation is enabled, to create it. - Execution through an agent or via is not supported.
Prepare Data
Run the following SQL in the taos CLI or taosExplorer to create an example supertable and insert a row. In the taos CLI, put each statement on a single line before running it. You do not need to create a topic manually.
CREATE DATABASE IF NOT EXISTS dataout_mysql_demo
VGROUPS 1
WAL_RETENTION_PERIOD 3600;
CREATE STABLE IF NOT EXISTS dataout_mysql_demo.meters (
ts TIMESTAMP,
current FLOAT,
voltage INT
) TAGS (
device VARCHAR(16)
);
INSERT INTO dataout_mysql_demo.meter01
USING dataout_mysql_demo.meters
TAGS ('meter01')
VALUES (NOW, 10.5, 220);
Create a MySQL Data Out Task
In taosExplorer, open Data Out, click Create New Task, enter a task name, and select MySQL as the data target type.
TDengine Subscription

- TDengine Endpoint: for example
tmq+ws://root:taosdata@localhost:6041/dataout_mysql_demo. The database in the path is the default database of the subscription SQL. - Subscription SQL: a single
SELECTquery. It follows TDengine query topic rules: query one table, child table or supertable, and project columns, tags,tbnameand scalar functions with an optionalWHERE. Aggregates, windows, JOIN, subqueries,UNION,DISTINCT,GROUP BY,ORDER BY,LIMITand virtual tables are not supported. - Start Position:
earliestincludes historical data,latestsubscribes only new data. It takes effect only on the first consumption of the consumer group. - Consumer Group ID: defaults to the task topic name. Changing it uses a new consumer group and rereads the data.
- TSDB Data: when enabled, persisted TSDB data is subscribed as well, not only WAL data. Together with the start position it determines what is read on the first start.
| Start Position | TSDB Data | Data read on the first start |
|---|---|---|
| earliest | on | All persisted historical data from TSDB, then WAL for new data |
| earliest | off | Only from the earliest position still retained in WAL |
| latest | any | Only data written after the task starts |
For this example, use SELECT ts, device, current, voltage FROM dataout_mysql_demo.meters. For more about query topics and consumer positions, see Data Subscription.
Connection and Authentication
Under Connection, enter the host, port (default 3306), and the name of an existing database such as rdb_out. Under Authentication, enter the database user and password.
- SSL Mode: Choose
DISABLED,PREFERRED, orREQUIREDto match the MySQL server’s TLS configuration.
Target Table

- Table: Enter the target table name, such as
meters_out. - Create Table: If enabled, taosX creates the table when it does not exist. For an existing table, taosX checks compatibility without changing the table definition.
- Storage engine: Existing target tables must use a transactional engine such as InnoDB. taosX rejects nontransactional tables such as MyISAM.
- Timestamp columns in existing tables: In automatic mode, use
DATETIMEfor millisecond or microsecond timestamps.TIMESTAMPis not supported because MySQL would convert the written values using the session time zone. - Primary Key: Fetch the result columns and select up to 16 columns. The form initially selects the first eligible column. In this example, select
tsanddeviceas the composite key and use a differentdevicevalue for each child table. If you select onlyts, rows from different child tables at the same timestamp can overwrite one another. Using the shortdevicetag instead oftbnamekeeps the key within the 767-byte limit that taosX applies to MySQL 5.6. - Timestamp Storage: With Auto, millisecond and microsecond timestamps use a date-time type and nanosecond timestamps use
BIGINT. With BIGINT, all timestamps are stored as raw integers. - Time Zone: Time zone for converting timestamps to date-time values. Leave this empty to use the taosX host’s local time zone.
Advanced
- Batch Size: maximum rows written per batch, default 1000.
- Batch Timeout (ms): maximum wait before a batch is written, default 1000.
- Write Timeout and Max Retry Time: configurable through DSN parameters, default to 60 and 180 seconds.
Connectivity Check and Preview
- Click Check Connection to check that the query returns a compatible result schema and that the target is reachable. This check does not create a topic. For query-topic restrictions, use the full preview or start the task.
- Set Row Count and Wait Time (s) and click Preview Data. The preview returns when it reaches either limit. Review the sample rows, the table creation statement (if a table needs to be created), and the write statement template. Preview does not write to the target database or alter the task’s consumer position.
- Click Submit and check the task status in the Data Out task list.
DSN Example
FROM 'tmq+ws://root:password@localhost:6041/dataout_mysql_demo?query=SELECT%20ts,%20device,%20current,%20voltage%20FROM%20dataout_mysql_demo.meters&auto.offset.reset=earliest'
TO 'mysql://writer:password@localhost:3306/rdb_out?table=meters_out&primary_key=ts,device'
Data publishing parameters for the target DSN:
| Parameter | Description | Default |
|---|---|---|
table | Target table name, required | — |
primary_key | Primary key columns separated by commas, required | — |
create_table | Whether to create the table when it does not exist | true |
timestamp_type | auto or bigint | auto |
time_zone | local, an IANA name or an offset such as +08:00 | local |
batch_size | Maximum rows per batch | 1000 |
batch_timeout | Maximum wait before a batch is written, e.g. 1000, 500ms, 2s | 1s |
write_timeout | Timeout of one batch write | 60s |
max_retry_time | Total retry time for a recoverable error, at most 240 seconds | 180s |
Type Mapping
When taosX creates the table, it maps result columns as follows:
| TDengine type | MySQL type |
|---|---|
TIMESTAMP (ms, us) | DATETIME(3) / DATETIME(6) |
TIMESTAMP (ns) | BIGINT |
BOOL | TINYINT(1) |
TINYINT | TINYINT |
SMALLINT / INT / BIGINT | same name |
TINYINT UNSIGNED | TINYINT UNSIGNED |
SMALLINT UNSIGNED | SMALLINT UNSIGNED |
INT UNSIGNED | INT UNSIGNED |
BIGINT UNSIGNED | BIGINT UNSIGNED |
FLOAT / DOUBLE | FLOAT / DOUBLE |
VARCHAR / NCHAR | VARCHAR(n) or TEXT |
VARBINARY / GEOMETRY | VARBINARY(n) or BLOB |
BLOB | LONGBLOB |
JSON (tags only) | JSON |
DECIMAL / DECIMAL64 | not supported |
Write Semantics
- Idempotent writes: rows are written with an UPSERT on the primary key, updating existing rows and inserting new ones. Task restarts, network interruptions and repeated delivery never create duplicate rows or stop the task with a primary key conflict.
- All columns are written: every result column of a row is written, including columns whose value is
NULL. When a column isNULLin TDengine, the target column is written asNULLtoo. - Repeated keys within one batch: only the last value is kept.
- Deletes are not propagated: data deleted in TDengine is kept in the target table.
- Tag values: tag values are read as they are in TDengine at consumption time.
Limitations
- One subscription SQL and one target table per task. Changing the SQL, the table, the primary key, the timestamp storage or the time zone requires a new task.
- A target table can be written by only one task and should have no other writers.
- A MySQL target table must not have unique indexes other than the primary key.
- The target table must have a primary key or unique constraint on exactly the configured primary key.
- DECIMAL output is not supported. Convert it in the subscription SQL, for example with
CAST(col AS VARCHAR(64)), which keeps the exact decimal text. - MySQL cannot store
NaNor±Inf; such values are written asNULLand counted in the metrics. - Data subject to a task stop or backlog longer than the WAL retention period of the source is skipped. taosX reports a warning in the task activity when it detects this; changing the consumer group ID and restarting resynchronizes all data.
- Topic columns are fixed at creation, so columns added to the source table later are not synchronized.
Performance and Observability
Task metrics include messages read, rows written, batches written, retries, write errors, elapsed time of the last write, values written as NULL because of NaN/Inf, WAL data gaps, and the consumer progress per vgroup.
Write throughput mainly depends on the target database and the row width, and can be improved by tuning the batch size and batch timeout.