@@ -72,3 +72,76 @@ make fmt
7272```
7373
7474See [ EMQX documentation] ( https://docs.emqx.com/en/enterprise/v5.0/extensions/plugins.html ) for details on how to deploy custom plugins.
75+
76+ ## Database Schema
77+
78+ This plugin requires a pre-defined database schema.
79+
80+ ### MySQL
81+
82+ ** Messages table**
83+
84+ ```
85+ CREATE TABLE IF NOT EXISTS `mqtt_msg` (
86+ `id` bigint unsigned NOT NULL AUTO_INCREMENT,
87+ `msgid` varchar(64) DEFAULT NULL,
88+ `topic` varchar(180) NOT NULL,
89+ `sender` varchar(64) DEFAULT NULL,
90+ `qos` tinyint(1) NOT NULL DEFAULT '0',
91+ `retain` tinyint(1) DEFAULT NULL,
92+ `payload` blob,
93+ `arrived` datetime NOT NULL,
94+ PRIMARY KEY (`id`),
95+ INDEX topic_index(`topic`)
96+ )
97+ ENGINE=InnoDB DEFAULT CHARSET=utf8MB4;
98+ ```
99+
100+ ** Subscriptions table**
101+
102+ ```
103+ CREATE TABLE IF NOT EXISTS `mqtt_sub` (
104+ `clientid` varchar(64) NOT NULL,
105+ `topic` varchar(180) NOT NULL,
106+ `qos` tinyint(1) NOT NULL DEFAULT '0',
107+ PRIMARY KEY (`clientid`, `topic`)
108+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8MB4;
109+ ```
110+
111+ ### Redis
112+
113+ Redis uses the following data structures (no predefined schema needed, structures are created automatically):
114+
115+ ** Subscriptions** - Redis Hashes
116+
117+ - Key pattern: ` mqtt:sub:{clientid} `
118+ - Hash fields: ` {topic} ` -> ` {qos} ` (integer)
119+ - Operations: ` HSET ` , ` HDEL ` , ` HGETALL `
120+
121+ ** Messages** - Redis Hashes
122+
123+ - Key pattern: ` mqtt:msg:{msgid} ` (msgid is base62 encoded)
124+ - Hash fields:
125+ - ` id ` -> base62 encoded message ID
126+ - ` from ` -> sender/clientid
127+ - ` qos ` -> QoS level (0, 1, or 2)
128+ - ` topic ` -> topic name
129+ - ` payload ` -> message payload (binary)
130+ - ` ts ` -> timestamp (integer)
131+ - ` retain ` -> "true" or "false" (string)
132+ - Operations: ` HMSET ` , ` HGETALL ` , ` DEL ` , ` EXPIRE `
133+
134+ ** Message Index by Topic** - Redis Sorted Sets
135+
136+ - Key pattern: ` mqtt:msg:{topic} `
137+ - Members: base62 encoded message IDs
138+ - Scores: timestamps (used for TTL/expiration cleanup)
139+ - Operations: ` ZADD ` , ` ZRANGE ` , ` ZREMRANGEBYSCORE ` , ` ZREM `
140+
141+ ** Required Redis Operations for ACL Control**
142+
143+ If using Redis ACL, the user needs permissions for the following operations:
144+
145+ - Hash operations: ` HSET ` , ` HDEL ` , ` HGETALL ` , ` HMSET ` , ` DEL ` , ` EXPIRE `
146+ - Sorted set operations: ` ZADD ` , ` ZRANGE ` , ` ZREMRANGEBYSCORE ` , ` ZREM `
147+ - Key pattern operations: Access to keys matching ` mqtt:sub:* ` and ` mqtt:msg:* `
0 commit comments