IMD SolutionComputing LibraryShop main store

Troubleshooting

Fix ECONNREFUSED when Node-RED connects to a remote SQL database

Edge computing and IIoT Published 7 min read

Short answer

ECONNREFUSED 127.0.0.1:3306 means the MySQL node dialed loopback on the machine running Node-RED, so the database computer was never contacted. Put the database computer's LAN address in the node's Host field, confirm MariaDB listens on that interface with TCP 3306 open, and create the account with the edge device's address as its host part.

On this page
  1. What does ECONNREFUSED 127.0.0.1:3306 actually mean?
  2. Where should the MySQL node's host field point?
  3. How do I know MariaDB is listening on the network?
  4. What does each connection error tell me to check next?
  5. How do I create the MariaDB account for the edge device?
  6. Why does the database name case matter after login works?
  7. What proves the whole path from MQTT to the table works?
  8. Questions asked

ECONNREFUSED 127.0.0.1:3306 in a Node-RED MySQL node almost always means the node is dialing its own loopback interface: 127.0.0.1 is the machine the Node-RED process runs on, so when the database lives on a separate computer, the request never leaves the edge device. The fix is three settings in order. The node's Host field gets the database computer's LAN address, MariaDB must listen on that interface with TCP 3306 reachable, and the MariaDB account must carry the edge device's source address as its host part. This walkthrough uses one example network — edge device 192.168.0.234, database computer 192.168.0.132, port 3306 — and shows the settings that turn the refusal into a working insert.

What does ECONNREFUSED 127.0.0.1:3306 actually mean? #

ECONNREFUSED 127.0.0.1:3306 means the MySQL node dialed TCP port 3306 on 127.0.0.1, which is always the machine running the Node-RED process itself — so the database computer was never contacted at all.

The MySQL node from node-red-node-mysql hands its configured host and port to a TCP connect call in the Node.js runtime beneath Node-RED, and a refusal names the exact destination that was dialed, address and port together. Nothing in that exchange touches credentials, SQL syntax, or table design; the request ends before any of them run.

Because the flow runs on the edge device, loopback points into that device, and a server installed on a separate computer is unreachable through it no matter how its accounts are written. Container deployments repeat the trap one level down, which is why rebooting the host PC from Node-RED in a container needs its own method: loopback there is the container's network namespace, not even the edge device's operating system.

Where should the MySQL node's host field point? #

Set the node's Host field to the database computer's LAN address — 192.168.0.132 in the example below — and leave the port at 3306 when the server listens there; 127.0.0.1 or localhost only works when the database runs on the same machine as Node-RED.

Work through the node in four steps:

  1. Read the refusal as the destination actually dialed, address and port together.
  2. Replace a Host value of 127.0.0.1 or localhost with the database computer's LAN address.
  3. Keep the Port value only if the server listens on it; 3306 is the MySQL and MariaDB default, and the refusal names the port the node used.
  4. Redeploy, then read the next error — each one names a different layer.

Worked example, illustrative values: the edge device is 192.168.0.234, the database computer is 192.168.0.132, and the server uses the default port.

  • Error before the change: connect ECONNREFUSED 127.0.0.1:3306, so the dialed destination was the pair (127.0.0.1, 3306).
  • Intended destination: the pair (192.168.0.132, 3306).
  • Port check: 3306 = 3306, a match. Address check: 127.0.0.1 ≠ 192.168.0.132, and 127.0.0.1 resolves inside the edge device, so the server's listener never saw the request.

The node's connection fields after the change:

Host:     192.168.0.132
Port:     3306
User:     edgeuser
Password: (the account's password)
Database: sensordata
Charset:  UTF8

Replace every value above with the installation's own; the server address belongs in Host and the database name in Database, and neither substitutes for the other. The same split between the runtime machine and the database server governs every engine an edge flow writes to, including logging production data to SQL Server.

How do I know MariaDB is listening on the network? #

Check three things on the database computer: the MariaDB service is running, its bind-address covers the LAN interface instead of loopback only, and the firewall allows inbound TCP 3306 from the edge device's address.

  • Service state: a stopped listener refuses every connection, so confirm the service is up before touching configuration.
  • bind-address: a server bound to loopback accepts only local clients, so the bind-address setting in the server's configuration file must cover the LAN interface.
  • Firewall: permit inbound TCP 3306 from the edge device's address on the computer's firewall and on any firewall between the two devices. Open the port for that one address — a database listener reachable from every device on the plant network is exposure nobody needs.
  • Port agreement: the port the server listens on must equal the node's Port field.

Then test from the edge device: open a TCP connection to 192.168.0.132 on port 3306. A connection that opens proves the listener and the path; a refusal points at the database computer; a timeout means packets are dropped in transit. Only the first result clears this stage.

What does each connection error tell me to check next? #

Read the error as the destination that was dialed: a refusal names an address and port nobody accepted, a timeout names a path packets never crossed, and an access-denied reply proves the TCP connection reached MariaDB.

Node-RED reports Layer it names Where to work
connect ECONNREFUSED 127.0.0.1:3306 Loopback dialed on the Node-RED host The node's Host field
connect ECONNREFUSED at the server's address and port Nothing accepted on that address and port MariaDB service, bind-address, port, firewall
Connection timeout Packets dropped or unrouted between the devices Routing and firewalls on the path
Access denied, or Host '...' is not allowed to connect TCP reached MariaDB and the account rejected the client Account host part, password, grants
Unknown database Login succeeded and the selected name does not exist as spelled The node's Database field

Each row is a decision: fix the layer the error names, redeploy, read again. An access-denied reply is progress, because it proves the network path, the listener, and the firewall all work.

How do I create the MariaDB account for the edge device? #

Create the account with the edge device's source address as the host part — CREATE USER 'edgeuser'@'192.168.0.234' — because MariaDB matches the username and the client address together, and 'edgeuser'@'localhost' does not match a remote client.

A MariaDB account is a pair: a username and the client address it answers to. The account for the example network, with illustrative names:

CREATE USER 'edgeuser'@'192.168.0.234' IDENTIFIED BY 'one-long-unique-password';
GRANT SELECT, INSERT, UPDATE ON sensordata.* TO 'edgeuser'@'192.168.0.234';

The host part is the source address MariaDB sees when the node connects — the edge device's own address when nothing rewrites it. Grants apply as soon as the statement runs. If routing or address translation changes the source, MariaDB names the address it actually saw in its rejection, and that observed address is the one to grant. An account written as 'user'@'%' matches every source address and works, but the single-address entry names the one client this database accepts, and that is the one to keep.

Why does the database name case matter after login works? #

Use the exact database name the server shows, character for character, because a case-only difference between the created name and the node's configured name can break database selection on servers where names are case-sensitive.

Case handling for database names depends on the server's platform and its settings. When the name was created under one spelling and configured in the node under another, login can succeed and database selection still fail. List the databases on the server, copy the spelling into the node's Database field exactly, and confirm the account holds privileges on that name. The Charset field sets the connection charset and has no effect on names or case.

What proves the whole path from MQTT to the table works? #

Deploy the flow and watch two things: the MySQL node's status shows connected under the database computer's address, and the target table's row count increases each time the MQTT input delivers a value.

  1. After deploy, the node's status settles on connected and the debug sidebar shows no new connection errors.
  2. Send one known reading through the flow and count rows: a table holding 120 rows before the message must hold 121 after it — 120 + 1 = 121 — with the new row carrying that value and a current timestamp.

If the connection stays healthy while no rows appear, the fault sits upstream of SQL; the place to start is fixing MQTT sensor data missing in Node-RED. The payload's shape — a bare number rather than a JSON object — only becomes relevant here, at the point the flow must place it into a valid INSERT statement.

Questions asked

Does the same loopback problem happen when Node-RED runs in a container?

Yes. Inside a container, 127.0.0.1 is the container's own network namespace, not the host and not the database computer. The fix is identical: put the database computer's reachable address in the node's Host field. On a default bridge network, the host machine itself is reached through the gateway address, never through loopback.

Why does a SQL client on the database computer connect while the edge device cannot?

A client running on the database computer connects through loopback, matches localhost accounts, and never crosses the network, so it proves nothing about remote reachability. The edge device must traverse routing, the listener's bind-address, the firewall on port 3306, and host-qualified account matching. Success from the server's own console is not evidence that any of those four layers works.

Does changing bind-address require restarting the database service?

Yes. The bind-address is read when the service starts, so editing the configuration file changes nothing until MariaDB is restarted. Plan that restart, because every client, local and remote, loses its connections while the service comes back up.

Can I fix ECONNREFUSED by changing the SQL statements or the MQTT payload?

No. The refusal happens at the TCP layer before any credential is checked or any SQL is parsed, so table definitions, INSERT statements, and payload shaping cannot influence it. Payload format only matters after the connection succeeds: a bare numeric MQTT value still has to be placed into a valid INSERT before rows appear.

Keep reading

  • How-to

    Log production data from a panel PC to SQL Server

    Run Node-RED on the panel PC, read the production values straight from the controller, and insert one row per event into SQL Server with the mssql-plus node. Keep the panel's CSV-plus-FTP file relay only as a fallback, block duplicates with a last-record check and a unique key, and expect about 86,400 rows, roughly 8.6 MB, per day at one record per second.

    Edge computing and IIoT

  • How-to

    Route MQTT sensor data into Grafana on an edge PC

    An MQTT broker forwards each message once and keeps no history, so Grafana cannot trend from it directly. Subscribe with Node-RED, write each payload into InfluxDB, set a 365-day retention policy, and point a Grafana data source at that database. For the example below — 720 signals sampling every 5 s — that is about 0.9 TB for a year; provision roughly 2 TB.

    Edge computing and IIoT

  • Troubleshooting

    Troubleshoot missing data in a Node-RED InfluxDB Grafana stack

    Work the chain in order: acquisition into Node-RED, the InfluxDB service and its storage mount, the write settings, then the Grafana query. In a 2.x stack most empty panels trace to a wrong token, bucket, URL or a controller clock that stamps points outside the panel's time range.

    Edge computing and IIoT