一 下载mosquitto镜像
docker pull eclipse-mosquitto:latest
二 创建数据目录
mkdir -pv /data/mosquitto/{config,data,logs}
三 修改mosquitto.conf
listener 1883 0.0.0.0
消息持久存储
persistence true
persistence_location /var/lib/mosquitto/日志文件
log_dest file /mosquitto/log/mosquitto.log其他配置
include_dir /etc/mosquitto/conf.d
禁止匿名访问
allow_anonymous false认证配置
password_file /mosquitto/auth/pwfile权限配置
acl_file /mosquitto/auth/aclfile
四 创建启动脚本
cat start_mqtt.sh
#!/bin/bash
docker rm -f mosquitto
docker run -d --name=mosquitto --privileged -p 1883:1883 -p 9001:9001 -v /data/mosquitto/:/mosquitto eclipse-mosquitto
五 设置认证
5.1 启动mosquitto
docker run --rm -it --name=mosquitto --privileged -p 1883:1883 -p 9001:9001 -v /data/mosquitto/:/mosquitto eclipse-mosquitto sh
5.2 创建pwfile
touch /mosquitto/auth/pwfile
5.3 创建用户
mosquitto_passwd /mosquitto/auth/pwfile test01
Password:
Reenter password:
5.4 创建权限
cat /mosquitto/auth/aclfile
user test01
topic write #
topic read #
5.5 重启mosquitto
./start_mqtt.sh
六 mosquitto_pub 命令参数说明
# mosquitto_pub --help
mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.
mosquitto_pub version 2.0.14 running on libmosquitto 2.0.14.
Usage: mosquitto_pub {[-h host] [--unix path] [-p port] [-u username] [-P password] -t topic | -L URL}
{-f file | -l | -n | -m message}
[-c] [-k keepalive] [-q qos] [-r] [--repeat N] [--repeat-delay time] [-x session-expiry]
[-A bind_address] [--nodelay]
[-i id] [-I id_prefix]
[-d] [--quiet]
[-M max_inflight]
[-u username [-P password]]
[--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
[{--cafile file | --capath dir} [--cert file] [--key file]
[--ciphers ciphers] [--insecure]
[--tls-alpn protocol]
[--tls-engine engine] [--keyform keyform] [--tls-engine-kpass-sha1]]
[--tls-use-os-certs]
[--proxy socks-url]
[--property command identifier value]
[-D command identifier value]
mosquitto_pub --help-A : bind the outgoing socket to this host/ip address. Use to control which interface
the client communicates over.
-d : enable debug messages.
-c : disable clean session/enable persistent client mode
When this argument is used, the broker will be instructed not to clean existing sessions
for the same client id when the client connects, and sessions will never expire when the
client disconnects. MQTT v5 clients can change their session expiry interval with the -x
argument.
-D : Define MQTT v5 properties. See the documentation for more details.
-f : send the contents of a file as the message.
-h : mqtt host to connect to. Defaults to localhost.
-i : id to use for this client. Defaults to mosquitto_pub_ appended with the process id.
-I : define the client id as id_prefix appended with the process id. Useful for when the
broker is using the clientid_prefixes option.
-k : keep alive in seconds for this client. Defaults to 60.
-L : specify user, password, hostname, port and topic as a URL in the form:
mqtt(s) 标签:.-,部署,will,--,mqtt,client,mosquitto,.-- From: https://blog.51cto.com/u_4028786/5789194