[Docker] [MQTT] 在 ubuntu 上使用 docker 搭建 EMQX

emqx 官网 https://www.emqx.io/

1. 安装 doker

参考 Ubuntu安装Docker及Docker的基本命令 - 简书

2. 拉取 emqx

$ docker pull emqx/emqx:v4.0.0

3. 启动 emqx

参考:https://hub.docker.com/r/emqx/emqx

$ sudo mkdir -p /data/log/emqx
$ sudo chmod -R 777 /data/log/emqx 
$ sudo docker run -d --name emqx31 -p 1883:1883 -p 8083:8083 -p 8883:8883 -p 8084:8084 -p 18083:18083 -p 8080:8080 \
--network bridge \
-e EMQX_LISTENER__TCP__EXTERNAL=1883 \
-e EMQX_LOADED_PLUGINS="emqx_auth_redis,emqx_recon,emqx_retainer,emqx_management,emqx_dashboard" \
-e EMQX_AUTH__REDIS__SERVER="192.168.1.1:6379" \
-e EMQX_AUTH__REDIS__PASSWORD="123456" \
-e EMQX_AUTH__REDIS__PASSWORD_HASH=plain \
-e EMQX_ALLOW_ANONYMOUS=false \
-e EMQX_LOG__DIR=/log \
-e EMQX_LOG__INFO__FILE="info.log" \
-e EMQX_LOG__ERROR__FILE="error.log" \
-v /data/log/emqx:/log \
emqx/emqx:v4.0.0

4. 请求 API 查看是否开启 redis 插件

参考 https://docs.emqx.io/broker/latest/cn/rest.html#plugins

curl --request GET   --url http://127.0.0.1:18083/api/v4/plugins   --header 'authorization: Basic YWRtaW46cHVibGlj' | jq

{
  "code": 0,
  "data": [
    {
      "node": "6fa9662848ff@172.17.0.2",
      "plugins": [
        {
          "name": "emqx_auth_clientid",
          "version": "4.0.0",
          "description": "EMQ X Authentication with ClientId/Password",
          "active": false,
          "type": "auth"
        },
        {
          "name": "emqx_auth_http",
          "version": "4.0.0",
          "description": "EMQ X Authentication/ACL with HTTP API",
          "active": false,
          "type": "auth"
        },
        {
          "name": "emqx_auth_jwt",
          "version": "4.0.0",
          "description": "EMQ X Authentication with JWT",
          "active": false,
          "type": "auth"
        },
        {
          "name": "emqx_auth_ldap",
          "version": "4.0.0",
          "description": "EMQ X Authentication/ACL with LDAP",
          "active": false,
          "type": "auth"
        },
        {
          "name": "emqx_auth_mongo",
          "version": "4.0.0",
          "description": "EMQ X Authentication/ACL with MongoDB",
          "active": false,
          "type": "auth"
        },
        {
          "name": "emqx_auth_mysql",
          "version": "4.0.0",
          "description": "EMQ X Authentication/ACL with MySQL",
          "active": false,
          "type": "auth"
        },
        {
          "name": "emqx_auth_pgsql",
          "version": "4.0.0",
          "description": "EMQ X Authentication/ACL with PostgreSQL",
          "active": false,
          "type": "auth"
        },
        {
          "name": "emqx_auth_redis",
          "version": "4.0.0",
          "description": "EMQ X Authentication/ACL with Redis",
          "active": true,
          "type": "auth"
        },
        {
          "name": "emqx_auth_username",
          "version": "4.0.0",
          "description": "EMQ X Authentication with Username and Password",
          "active": false,
          "type": "auth"
        },
        {
          "name": "emqx_bridge_mqtt",
          "version": "4.0.0",
          "description": "EMQ X Bridge to MQTT Broker",
          "active": false,
          "type": "bridge"
        },
        {
          "name": "emqx_coap",
          "version": "4.0.0",
          "description": "EMQ X CoAP Gateway",
          "active": false,
          "type": "protocol"
        },
        {
          "name": "emqx_dashboard",
          "version": "4.0.0",
          "description": "EMQ X Web Dashboard",
          "active": true,
          "type": "feature"
        },
        {
          "name": "emqx_delayed_publish",
          "version": "4.0.0",
          "description": "EMQ X Delayed Publish",
          "active": false,
          "type": "feature"
        },
        {
          "name": "emqx_lua_hook",
          "version": "4.0.0",
          "description": "EMQ X Lua Hooks",
          "active": false,
          "type": "feature"
        },
        {
          "name": "emqx_lwm2m",
          "version": "4.0.0",
          "description": "EMQ X LwM2M Gateway",
          "active": false,
          "type": "protocol"
        },
        {
          "name": "emqx_management",
          "version": "4.0.0",
          "description": "EMQ X Management API and CLI",
          "active": true,
          "type": "feature"
        },
        {
          "name": "emqx_plugin_template",
          "version": "4.0.0",
          "description": "EMQ X Plugin Template",
          "active": false,
          "type": "feature"
        },
        {
          "name": "emqx_psk_file",
          "version": "4.0.0",
          "description": "EMQX PSK Plugin from File",
          "active": false,
          "type": "feature"
        },
        {
          "name": "emqx_recon",
          "version": "4.0.0",
          "description": "EMQ X Recon Plugin",
          "active": true,
          "type": "feature"
        },
        {
          "name": "emqx_reloader",
          "version": "4.0.0",
          "description": "EMQ X Reloader Plugin",
          "active": false,
          "type": "feature"
        },
        {
          "name": "emqx_retainer",
          "version": "4.0.0",
          "description": "EMQ X Retainer",
          "active": true,
          "type": "feature"
        },
        {
          "name": "emqx_rule_engine",
          "version": "4.0.0",
          "description": "EMQ X Rule Engine",
          "active": false,
          "type": "feature"
        },
        {
          "name": "emqx_sn",
          "version": "4.0.0",
          "description": "EMQ X MQTT-SN Plugin",
          "active": false,
          "type": "protocol"
        },
        {
          "name": "emqx_statsd",
          "version": "4.0.0",
          "description": "Statsd for EMQ X",
          "active": false,
          "type": "feature"
        },
        {
          "name": "emqx_stomp",
          "version": "4.0.0",
          "description": "EMQ X Stomp Protocol Plugin",
          "active": false,
          "type": "protocol"
        },
        {
          "name": "emqx_web_hook",
          "version": "4.0.0",
          "description": "EMQ X Webhook Plugin",
          "active": false,
          "type": "feature"
        }
      ]
    }
  ]
}

5. 登录 redis 添加账号

参考 https://docs.emqx.io/broker/latest/cn/plugins.html#redis

HSET mqtt_user: password “passwd”

192.168.1.1:6379> HSET mqtt_user:user01 password "123456

6. 安装 mosquitto 测试是否正常连接 MQTT

参考 MQTT mosquitto

 $ mosquitto_sub -v -t test -u user01 -P 123456
 $ mosquitto_pub -t test -m message -u user01 -P 123456

tangzixiang