在閱讀該教程以前,假定你已經瞭解 MQTT、MQTT 5 以及 EMQ X 的簡單知識。html
emqx-auth-mysql 它經過檢查每一個終端接入的 username
和 password
是否與用戶指定 的 MySQL 數據庫中存儲的信息一致來實現對終端的訪問控制。其功能邏輯以下:mysql
本文假設 MySQL 已經安裝在您的機器上,而且您能夠鏈接到 MySQL 服務器。注:EMQ X 開源版至 v3.1-beta.2 爲止,尚不支持 MySQL 8.0,所以如下內容僅適用於 MySQL 5.7 及如下版本。git
$ mysql --version mysql Ver 14.14 Distrib 5.7.25, for macos10.14 (x86_64) using EditLine wrapper
emqx-auth-mysql
提供了 mqtt.sql 文件幫助用戶快速建立數據表以及導入默認數據。mqtt.sql 將會爲 mqtt_acl
數據表導入如下默認規則:github
mysql> select * from mqtt_acl; +----+-------+-----------+-----------+----------+--------+--------+ | id | allow | ipaddr | username | clientid | access | topic | +----+-------+-----------+-----------+----------+--------+--------+ | 1 | 1 | NULL | $all | NULL | 2 | # | | 2 | 0 | NULL | $all | NULL | 1 | $SYS/# | | 3 | 0 | NULL | $all | NULL | 1 | eq # | | 4 | 1 | 127.0.0.1 | NULL | NULL | 2 | $SYS/# | | 5 | 1 | 127.0.0.1 | NULL | NULL | 2 | # | | 6 | 1 | NULL | dashboard | NULL | 1 | $SYS/# | +----+-------+-----------+-----------+----------+--------+--------+ 6 rows in set (0.00 sec)
allow - 1: allow; 0: denysql
access - 1: subscribe; 2: publish; 3: publish and subscribe數據庫
以上規則分別表示:macos
除此以外,用戶能夠導入自定義的 ACL 規則。服務器
Mac 環境安裝 mosquittoapp
brew install mosquitto
測試
建立數據庫,導入數據
mqtt.sql 路徑可根據實際狀況自行改動
mysql> create database mqtt; mysql> use mqtt; mysql> source ./emqx_auth_mysql/mqtt.sql mysql> insert into mqtt_user (id, is_superuser, username, password, salt) -> values (1, false, 'test', 'password', 'salt'); mysql> insert into mqtt_acl (id, allow, ipaddr, username, clientid, access, topic) -> values (7, 0, NULL, 'test', NULL, 1, 'mytopic'); mysql> exit;
修改配置文件
禁止匿名訪問:
## .../etc/emqx.conf allow_anonymous = false
配置數據庫中密碼的加密方式爲 plain
,即不加密:
## .../etc/plugins/emqx_auth_mysql.conf auth.mysql.password_hash = plain
配置要訪問的數據庫以及用戶名密碼:
## .../etc/plugins/emqx_auth_mysql.conf auth.mysql.username = root auth.mysql.password = public auth.mysql.database = mqtt
啓動 EMQ X 與 emqx-auth-mysql
$ ./_rel/emqx/bin/emqx start emqx 3.1 is started successfully! $ ./_rel/emqx/bin/emqx_ctl plugins load emqx_auth_mysql
測試
使用正確的用戶名和密碼進行鏈接,並訂閱 "topic" 主題
$ mosquitto_sub -p 1883 -u test -P password -t 'topic' -d Client mosqsub|91114-zhouzibod sending CONNECT Client mosqsub|91114-zhouzibod received CONNACK Client mosqsub|91114-zhouzibod sending SUBSCRIBE (Mid: 1, Topic: topic, QoS: 0) Client mosqsub|91114-zhouzibod received SUBACK Subscribed (mid: 1): 0
現象:鏈接並訂閱成功
使用錯誤的用戶名或密碼進行鏈接,並訂閱 "topic" 主題
$ mosquitto_sub -p 1883 -u bad_user -P password -t 'topic' -d Client mosqsub|91136-zhouzibod sending CONNECT Client mosqsub|91136-zhouzibod received CONNACK Connection Refused: not authorised.
現象:鏈接被拒絕
使用正確的用戶名和密碼進行鏈接,並訂閱 "#" 主題
$ mosquitto_sub -p 1883 -u test -P password -t '#' -d Client mosqsub|11257-zhouzibod sending CONNECT Client mosqsub|11257-zhouzibod received CONNACK Client mosqsub|11257-zhouzibod sending SUBSCRIBE (Mid: 1, Topic: #, QoS: 0) Client mosqsub|11257-zhouzibod received SUBACK Subscribed (mid: 1): 128
現象:鏈接成功,訂閱失敗,緣由碼128
使用正確的用戶名和密碼進行鏈接,並訂閱 "mytopic" 主題
$ mosquitto_sub -p 1883 -u test -P password -t 'mytopic' -d Client mosqsub|13606-zhouzibod sending CONNECT Client mosqsub|13606-zhouzibod received CONNACK Client mosqsub|13606-zhouzibod sending SUBSCRIBE (Mid: 1, Topic: mytopic, QoS: 0) Client mosqsub|13606-zhouzibod received SUBACK Subscribed (mid: 1): 128
現象:鏈接成功,訂閱失敗,緣由碼128