0x02 communication auth_error ai_generated true

MQTT CONNACK returned with identifier rejected (0x02) or server unavailable (0x03)

ID: communication/mqtt-publish-identifier-in-use

Also available as: JSON · Markdown · 中文
83%Fix Rate
84%Confidence
1Evidence
2024-02-28First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Mosquitto 2.0.15 active
EMQX 5.3 active
AWS IoT Core active
Paho MQTT 1.6 active

Root Cause

MQTT broker rejected the client connection because the client identifier is already in use with a clean session not set, or the broker's connection limit has been reached.

generic

中文

MQTT 代理拒绝客户端连接,因为客户端标识符已被使用且未设置清除会话,或代理的连接数已达上限。

Official Documentation

https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901081

Workarounds

  1. 90% success Set the clean session flag to true in the CONNECT packet, e.g., in Paho Python: `client.connect(broker, port, keepalive=60, clean_session=True)`.
    Set the clean session flag to true in the CONNECT packet, e.g., in Paho Python: `client.connect(broker, port, keepalive=60, clean_session=True)`.
  2. 95% success Generate a unique client ID for each connection using a UUID or device-specific identifier, e.g., `client_id = f"device-{uuid.uuid4()}"`.
    Generate a unique client ID for each connection using a UUID or device-specific identifier, e.g., `client_id = f"device-{uuid.uuid4()}"`.
  3. 85% success Increase the maximum number of concurrent connections on the broker, e.g., in Mosquitto: add `max_connections 1000` to `mosquitto.conf`.
    Increase the maximum number of concurrent connections on the broker, e.g., in Mosquitto: add `max_connections 1000` to `mosquitto.conf`.

中文步骤

  1. 在 CONNECT 数据包中将清除会话标志设置为 true,例如在 Paho Python 中:`client.connect(broker, port, keepalive=60, clean_session=True)`。
  2. 为每个连接生成唯一的客户端 ID,使用 UUID 或设备特定标识符,例如 `client_id = f"device-{uuid.uuid4()}"`。
  3. 增加代理上的最大并发连接数,例如在 Mosquitto 中:在 `mosquitto.conf` 中添加 `max_connections 1000`。

Dead Ends

Common approaches that don't work:

  1. Reconnect with the same client ID repeatedly without changing anything 85% fail

    The broker continues to reject the same client ID if the session is still active or limit persists.

  2. Disable all security settings on the broker (e.g., remove authentication) 90% fail

    May violate security policies and does not address the client ID conflict or connection limit.

  3. Use a random client ID on every reconnect without setting clean session 70% fail

    Random IDs may still conflict if not unique; without clean session, the broker retains state for each ID, potentially exhausting resources.