Troubleshooting
Why Mosquitto 2.0 rejects a Telegraf MQTT output: identifier rejected
Short answer
A Telegraf v1.19.3 MQTT output that works against Mosquitto 1.5 but fails against 2.0.12 with identifier rejected is sending a keepalive of zero: the plugin hard-codes it, and Mosquitto 2.x refuses an unlimited keepalive. MQTT 3.1.1 has no server-assigned keepalive, so the refusal leaves as CONNACK return code 2. Give the client a nonzero keepalive, by upgrading Telegraf or patching the line, and keep the 2.0.12 broker.
On this page
Telegraf v1.19.3, built from source, publishes to a Mosquitto 1.5 container without trouble. Point the same binary at a Mosquitto 2.0.12 container, same [[outputs.mqtt]] block, same Docker bridge, same port, and the output dies with identifier rejected. The client identifier is not the problem. The MQTT output at that tag hard-codes a keepalive of 0 seconds (opts.KeepAlive = 0 in plugins/outputs/mqtt/mqtt.go); Mosquitto 2.x refuses an unlimited keepalive, and MQTT 3.1.1 gives the broker no polite way to say so, so it answers with CONNACK return code 2, the code whose name sends you to the wrong field. The repair is one nonzero keepalive at the client. The 2.0.12 broker stays where it is.
Where the connection actually dies #
A publish travels from the Telegraf process, through its container interface and the Docker bridge, into the broker container, to the listener on TCP 1883, through CONNECT parsing, and back as CONNACK along the same path. Four points can stop it, and each leaves a different signature.
| Stopped at | Telegraf reports | Broker log shows |
|---|---|---|
| No route to the broker container | dial timeout | nothing |
| No listener reachable from the bridge | connection refused | nothing |
| Credentials or ACL | bad user name or password, not authorized | the connection, then a refusal |
| A field inside CONNECT | identifier rejected |
the connection, then CONNACK with a nonzero return code |
This fault is the last row, and two log lines prove it. New connection from <telegraf-ip> on port 1883 shows the TCP path through the bridge is intact, so routing is cleared. The CONNACK line carries the verdict: the accepted 1.5 run logs Sending CONNACK to Telegraf-Output-krVXp (0, 0), and the refused run shows the same client with a nonzero second value.
If the broker log stays empty #
Two branches sit below MQTT entirely.
No listener. Mosquitto 2.0 serves local connections only until you configure a listener, so a client on the Docker bridge is refused before any packet is parsed. Add the listener to /mosquitto/config/mosquitto.conf and restart the container:
listener 1883
allow_anonymous true
That second line belongs on an isolated test bridge only; a production listener needs passwords or TLS, and how to run a secure Mosquitto MQTT lab broker with Node-RED shows that pattern.
A stale container address. Recreating the broker, exactly what the 1.5 to 2.0.12 swap does, can change its bridge address, so re-read it with docker inspect, or drop the dependency by publishing the port with docker run -d --name mqttserver2 -p 1883:1883 eclipse-mosquitto:2.0.12. Inside a container, localhost names the container itself, so give the output the broker's real address; how to fix Telegraf MQTT connecting to localhost in Docker covers that trap. The broker's real address is also the lesson from a plant whose MQTT sink stayed offline until it used the broker's IP address.
The return code names the wrong field #
The MQTT 3.1.1 specification gives CONNACK six return codes: 0 accepted, 1 unacceptable protocol version, 2 identifier rejected, 3 server unavailable, 4 bad user name or password, 5 not authorized. Telegraf prints the text of the code it receives, so the error you see means code 2 arrived.
The textbook cause of code 2 is a client identifier the broker will not register. That is not what happens here. Telegraf builds its id as Telegraf-Output- plus a random suffix, and the 1.5 broker accepted that exact string. If you set a custom client id in the plugin, test once with it removed; when the refusal survives, the identifier is cleared and the keepalive field is next.
The zero inside the CONNECT packet #
The keepalive field of CONNECT tells the broker how long the session may sit silent. Zero is a special value: never time this client out. The v1.19.3 output hard-codes it, opts.KeepAlive = 0 in plugins/outputs/mqtt/mqtt.go, a line that arrived with the commit titled "Disable keepalive in mqtt output" (#3779). Mosquitto 1.5 accepts the zero. Mosquitto 2.0.12 refuses it, because the 2.x line enforces a maximum keepalive and an unlimited request sits above every ceiling.
The refusal reads wrong because the protocol boxes the broker in. MQTT 5 lets a broker answer an oversized keepalive with a server-assigned value instead of a refusal; the 3.1.1 CONNACK has no field for that, leaving accept-or-refuse, and the nearest refusal code is 2. Both projects track the behavior: Mosquitto issue #2309, "Keep alive 0 results in identifier rejected", and Telegraf issue #9763.
The requested keepalive is readable once a broker accepts the connection: the k value in the connect line's tail is the keepalive in seconds, and the unmodified build shows k0, the same zero the 2.0.12 broker refuses. After the patch below, that line shows k30 and the session carries PINGREQ traffic again.
Three tests that pin the cause #
Run these in order; each one closes a branch.
- Change only the broker. Keep the build, config, network and port identical and swap
eclipse-mosquitto:1.5foreclipse-mosquitto:2.0.12. The output fails only on the new image, which moves the cause out of your stack and into broker behavior. - Connect a second client. An unmodified Node-RED connects to the same 2.0.12 broker without changes. The broker is healthy; the difference lives inside Telegraf's CONNECT.
- Remove the zero. Comment out
opts.KeepAlive = 0inplugins/outputs/mqtt/mqtt.go, rebuild withmake, and connect to both brokers. The patched build is accepted by 1.5 and 2.0.12 alike, measured, not inferred. The trigger is the zero.
If step 3 still fails with a nonzero keepalive, stop blaming keepalive: a different CONNECT field or a broker policy is refusing the session. Capture the CONNECT on the bridge and compare it, field by field, with a client that connects.
Worked example: timing a dead session at keepalive 30 seconds and at zero #
The broker-side fix below has a price; this is the arithmetic that sets it.
Inputs:
- Keepalive 30 s, the value the current Telegraf output source sets (
KeepAlive: 30inplugins/outputs/mqtt/mqtt.go), and a sound target for a patched build. - Factor 1.5, the MQTT 3.1.1 rule that a server which has heard nothing from a client for one and a half keepalive periods must treat the connection as failed and drop it.
- Keepalive 0 s, what the unmodified v1.19.3 output sends; the same rule defines zero as no keepalive at all, so the timeout never starts.
Rule: a vanished client is detected after one and a half times its keepalive.
Arithmetic, patched client: 1.5 × 30 s = 45 s.
Arithmetic, unmodified client on a broker that tolerated it: 1.5 × 0 s = 0 s, which the protocol means not as an instant drop but as no bound at all.
Result: with the patch, a Telegraf container that dies without closing its socket leaves the broker's session table within 45 seconds. With the zero, that session lingers until something else exposes the dead socket, a broker restart usually.
Decision: any keepalive of 1 s or more buys a finite detection time; zero buys none. Keep the client nonzero and leave the broker's enforcement switched on.
The fix to deploy #
| Fix | What it buys | What it costs |
|---|---|---|
| Upgrade Telegraf to a current release | the output speaks a nonzero keepalive; both brokers accept it | leaving v1.19.3, ordinary release work |
Patch v1.19.3: comment out opts.KeepAlive = 0, or replace the zero with an explicit nonzero value |
accepted on 1.5 and 2.0.12, as test 3 measured | you carry a patched build until you upgrade |
Loosen the broker: max_keepalive in mosquitto.conf |
the unmodified client connects | dead-peer detection stays off for that client, and the broker is bent around one old build |
| Run Mosquitto 1.5 | accepts the zero | an old broker; test bench only |
The commit that disabled keepalive records no reason beyond its title, and the current source has returned to a nonzero default, so the patch moves with upstream rather than against it.
After the change, verify end to end: run ./telegraf -config /etc/telegraf/telegraf.conf --debug true against 2.0.12 and confirm two things: the broker log's connect line shows a k value above zero, and metrics leave the buffer in the Telegraf debug output.
The decision for each case #
- You build or update Telegraf, the case at hand: take the client-side fix. Upgrade, or comment out the zero and rebuild; either ends the fault on 2.0.12 and keeps both brokers interchangeable.
- The client is sealed, an appliance you can neither rebuild nor upgrade: use the broker's
max_keepaliveallowance for exactly as long as that appliance lives, put its replacement on the plan, and accept that its silent sessions linger. - Never park production on Mosquitto 1.5 to dodge this fault; the old image hides it and freezes the broker line.
Questions asked
Will other devices on the plant network fail the same way after a Mosquitto 2.0 upgrade?
Only clients that request a keepalive of zero. Every client with a nonzero keepalive connects as before, so the blast radius of a broker upgrade is your list of zero-keepalive devices. The same signature has taken out whole fleets overnight: after one home-automation platform updated its bundled broker, devices whose factory default was a zero keepalive all returned identifier rejected at once while configured clients kept working. Count those devices before you upgrade a shared broker.
Does switching on TLS or a password change the result?
No. The same setup was repeated with TLS enabled and the broker still answered identifier rejected, which clears the transport and leaves the CONNECT's keepalive field as the trigger. Credential faults carry their own return codes, so the identifier-rejected code arriving over TLS is still this fault, not a certificate or password problem.
What should I watch after giving the output a nonzero keepalive?
Reconnects on your worst link. A nonzero keepalive obliges the client to exchange a ping inside every keepalive window, and a link that added latency silently, such as a cellular backup or a loaded WAN, can start showing ping timeouts the zero never produced. If that appears, raise the keepalive instead of returning to zero; dead-session detection stretches with it, per the one-and-a-half rule in the worked example.
Do Telegraf's other MQTT plugins carry the same zero?
Treat each one as unknown until tested. The hard-coded zero lives in the output plugin's file at tag v1.19.3; other MQTT plugins are separate code with their own settings, not copies of that file. After a broker upgrade, connect every Telegraf MQTT plugin once, inputs and outputs, and read the broker log for a clean connect line before you call the migration finished.