How-to
Save Node-RED OPC UA values to MySQL through a template insert
Short answer
Wire the OPC UA client, a Template node and the MySQL node in series. The Template node builds INSERT INTO test.prosys_table (Counter, Time) VALUES from the measured value plus Now(), and places the finished statement on msg.topic, because the MySQL node reads its query from msg.topic and returns results in msg.payload. Then prove the row with a SELECT.
On this page
- Why does the OPC UA client's output never reach MySQL on its own?
- How do you wire the flow from the OPC UA server to MySQL?
- What does the OPC UA client put in msg.payload?
- How does the Template node build the INSERT statement?
- Why must the SQL query sit in msg.topic?
- What else can make a working insert fail?
- How do you verify the row landed in MySQL?
- Questions asked
Saving an OPC UA value into MySQL from Node-RED takes three nodes: the OPC UA client that subscribes to the variable, a Template node that builds the INSERT statement, and the MySQL node that executes it. The step people miss is the template's output property. The MySQL node runs whatever query it finds in msg.topic, while the OPC UA client hands the value over in msg.payload, so the template must assemble the statement and place it on msg.topic. Wire client to template to database, point the template's output at msg.topic, and prove the row with a SELECT before you call the flow done.
Why does the OPC UA client's output never reach MySQL on its own? #
Because the two nodes keep different contracts: the MySQL node executes the SQL text it finds in msg.topic and returns results in msg.payload, while the OPC UA client delivers the subscribed value in msg.payload, so nothing turns your measurement into a query until a Template node does.
Each node opens its own connection. The client speaks OPC UA to the server over the endpoint URL you configured; the MySQL node opens a MySQL session to the database and waits for SQL text. Node-RED passes messages between them but converts nothing: a completed read or a live subscription proves only the first hop. That is why the build needs the middle node, and why most stalled flows stall at the statement, not at the wiring.
How do you wire the flow from the OPC UA server to MySQL? #
Wire the OPC UA client, a Template node and the MySQL node in series, set the template's output property to msg.topic, store the database host, port and credentials in the MySQL node's configuration, and watch each hop with a Debug node.
- Install both palettes: node-red-contrib-opcua for the OPC UA client and node-red-node-mysql for the database.
- Point the client at the endpoint URL the server itself announces — for example
opc.tcp://192.168.1.50:4840— and set its action to subscribe so the flow fires on every value change. - Configure the MySQL node with the database host, the port it listens on (3306 unless it was changed) and credentials for an account that may write to the target table. If Node-RED runs on another machine or in a container, its localhost is not the database's localhost.
- Wire client to Template to MySQL, and tap a Debug node after the client and again between Template and MySQL.
- Set the Template node's Property field to msg.topic; the default msg.payload is the single most common reason this build never inserts.
- Deploy, let the subscription fire, and confirm a message appears at every Debug tap.
Two failures surface at this stage, before any SQL exists. If the client never connects at all, the Node-RED OPC UA troubleshooting checklist walks endpoint, security and namespace checks in order. If the MySQL node reports ECONNREFUSED, the database refused the TCP connection itself, and the Node-RED MySQL ECONNREFUSED fix narrows host, port and bind address before you touch the flow.
What does the OPC UA client put in msg.payload? #
Attach a Debug node to the client's output and read the complete message before you write any SQL: the payload may be the plain value, an object that wraps the value with its type, quality and timestamps, or an array when the subscription covers several items.
node-red-contrib-opcua is built on the node-opcua library, and the message shape depends on the action you chose and the version you installed. Three shapes cover nearly every case:
- A scalar number: the template substitutes it directly.
- An object: reference the field that holds the value, for example
{{payload.value}}, where value is an example field name — use whatever the Debug panel shows. - An array, one entry per subscribed item: select the element you want before the template.
Filter before you insert. A connection event or a Bad-quality notification is not a measurement, and dropping it here keeps junk rows out of the table.
How does the Template node build the INSERT statement? #
Drop a Template node between the client and the database, change its output property from the default msg.payload to msg.topic, and paste in the statement that renders the value into the row: INSERT INTO test.prosys_table (Counter, Time) VALUES ('{{payload}}', Now()).
test is the database, prosys_table the table, Counter and Time the columns of the example schema; use your own database and table names where they differ. The single quotes around the double-brace placeholder make the value an SQL string literal, which a numeric Counter column stores as the number it represents. Now() is evaluated by MySQL at execution time, so the Time column must be a date or timestamp type, and it records receipt time at the database, not the moment the server sampled the value — the same distinction that separates a PI value timestamp from its receipt time. If the row needs the source timestamp, carry it in a second field and insert it explicitly.
Hand-writing the INSERT text into a client's configuration is not a Node-RED quirk — configuring the SQL insert statement text in a TwinCAT MySQL client follows the same pattern, with the same cautions about literals and table names.
For text or externally influenced values, prefer the parameterized form the MySQL node also documents: put ? placeholders in the statement and pass the values as an array in msg.payload, which that node reserves for bind values. A placeholder inside quotes is still a quoted literal; template escaping is not parameter binding.
Why must the SQL query sit in msg.topic? #
Because the node-red-node-mysql documentation states the contract directly: msg.topic must hold the query for the database and the result comes back in msg.payload, so a finished INSERT left on msg.payload is never executed and the node has nothing to send.
This is why the template's Property field decides the flow, not the wire between the nodes. The fastest proof is a Debug node set to show the complete msg object immediately before the database node: msg.topic should read INSERT INTO test.prosys_table (Counter, Time) VALUES ('42', Now()), with your live value where the example shows 42. If it still shows the literal double-brace placeholder, the payload that reached the template was not the value — go back to the Debug tap on the client and fix the mapping.
Other database nodes use the same style of property contract with their own specifics. If the destination is SQL Server instead of MySQL, the OPC server to SQL Server logging pattern applies the same idea with that node's documented query property.
What else can make a working insert fail? #
With the statement rendering correctly, the remaining failures live on the database side — a missing table, a column name that does not match, an account that may connect but not insert, or a value that breaks a column rule — and the node can answer all of them with silence.
A MySQL node can emit no output message at all when the database rejects a statement, so treat an absent Debug message after it as a failed insert rather than a quiet success. Work the checklist in order:
- Schema: the database and the table exist, and the column names in the statement match the table exactly.
- Privileges: the account holds INSERT on that table; opening a connection proves nothing about write rights.
- Value: the substituted literal fits the column — a numeric string in a numeric column converts, an overlong or wrong-typed string does not.
- Time column: defined as date or timestamp so Now() lands, with the stored time following the database session's time zone rather than the PC running Node-RED.
How do you verify the row landed in MySQL? #
Trigger the flow once, then ask the database directly with SELECT * FROM test.prosys_table ORDER BY Time DESC LIMIT 1; and confirm the Counter column holds the value the server published and Time holds the moment MySQL executed the insert.
If the row comes back with the right number and a fresh timestamp, run the flow again and watch the row count climb by one per update: the path is proven and you can leave it deployed. If nothing comes back, the fault sits at one of three checkpoints, in this order — the value in msg.payload, the statement on msg.topic, or the database's schema and grants.
One number decides how long you can leave it running: the subscription rate. At one value update per second the flow inserts 60 × 60 × 24 = 86,400 rows per day; at ten updates per second it inserts 86,400 × 10 = 864,000 rows per day. A plain insert-per-update flow has no batching, so set the table's retention with that figure in hand — the database, not Node-RED, sets the ceiling.
That is the whole decision: the row present with the published value, deploy it; silence after the database node, work payload, then topic, then database.
Questions asked
Which privileges does the MySQL account used by this flow need?
The account named in the MySQL node's configuration must hold INSERT on the target table in addition to the right to connect; a login that only opens a session fails every statement silently. If the same flow reads rows back to verify inserts, grant SELECT on the same table as well. Set both through normal database administration and keep the flow account out of administrative roles.
What time zone does the Now() timestamp land in?
MySQL evaluates Now() in the session time zone of the connection the node opened, so the stored time follows the database server's zone setting rather than the clock of the PC running Node-RED. If your plant records history in UTC, set the session or server time zone to match, or the same instant will shift whenever a server setting changes.
Can one template insert several OPC UA values from a single message?
Yes. Reference each field of the incoming message once per column, using the exact field names the Debug panel shows, and keep one VALUES tuple per message so a row always maps to one sample. For several subscribed items arriving as an array, pick the elements into separate fields first, or build a multi-row statement only when every element is valid.
Does the pattern change when the source is a live PLC instead of a simulation server?
No. The client node, the template and the msg.topic contract stay identical; only the endpoint URL and the NodeId of the variable change. Take the endpoint from the OPC server configuration on the PLC side, subscribe to the same variable you would read in a test client, and the insert statement needs no edits at all.