# ll
total 556
-rw-r----- 1 sk sk 148417 Mar 6 14:59 libuv-1.44.2-1.el7.x86_64.rpm
-rw-r----- 1 sk sk 120717 Mar 6 14:59 libwebsockets-3.0.1-2.el7.x86_64.rpm
-rw-r----- 1 sk sk 293429 Mar 6 14:59 mosquitto-1.6.10-1.el7.x86_64.rpm
将用到的依赖上传到主机上,分别安装lib*类依赖
1.安装libuv-1.44.2-1.el7.x86_64.rpm
# sudo yum install libuv-1.44.2-1.el7.x86_64.rpm -y
Loaded plugins: fastestmirror
Examining libuv-1.44.2-1.el7.x86_64.rpm: 1:libuv-1.44.2-1.el7.x86_64
Marking libuv-1.44.2-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package libuv.x86_64 1:1.44.2-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================
Installing:
libuv x86_64 1:1.44.2-1.el7 /libuv-1.44.2-1.el7.x86_64 393 k
Transaction Summary
==================================================================================================================================================
Install 1 Package
Total size: 393 k
Installed size: 393 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:libuv-1.44.2-1.el7.x86_64 1/1
Verifying : 1:libuv-1.44.2-1.el7.x86_64 1/1
Installed:
libuv.x86_64 1:1.44.2-1.el7
Complete!
2.安装libwebsockets-3.0.1-2.el7.x86_64.rpm
# sudo yum install libwebsockets-3.0.1-2.el7.x86_64.rpm -y
Loaded plugins: fastestmirror
Examining libwebsockets-3.0.1-2.el7.x86_64.rpm: libwebsockets-3.0.1-2.el7.x86_64
Marking libwebsockets-3.0.1-2.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package libwebsockets.x86_64 0:3.0.1-2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================
Installing:
libwebsockets x86_64 3.0.1-2.el7 /libwebsockets-3.0.1-2.el7.x86_64 269 k
Transaction Summary
==================================================================================================================================================
Install 1 Package
Total size: 269 k
Installed size: 269 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libwebsockets-3.0.1-2.el7.x86_64 1/1
Verifying : libwebsockets-3.0.1-2.el7.x86_64 1/1
Installed:
libwebsockets.x86_64 0:3.0.1-2.el7
Complete!
3.最后安装mqtt
# sudo yum install -y mosquitto-1.6.10-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining mosquitto-1.6.10-1.el7.x86_64.rpm: mosquitto-1.6.10-1.el7.x86_64
Marking mosquitto-1.6.10-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mosquitto.x86_64 0:1.6.10-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================
Installing:
mosquitto x86_64 1.6.10-1.el7 /mosquitto-1.6.10-1.el7.x86_64 748 k
Transaction Summary
==================================================================================================================================================
Install 1 Package
Total size: 748 k
Installed size: 748 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mosquitto-1.6.10-1.el7.x86_64 1/1
Verifying : mosquitto-1.6.10-1.el7.x86_64 1/1
Installed:
mosquitto.x86_64 0:1.6.10-1.el7
Complete!
mosquitto的启动检查命令
service mosquitto start
service mosquitto stop
service mosquitto restart
service mosquitto status
检查运行状态
# service mosquitto status
Redirecting to /bin/systemctl status mosquitto.service
● mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
# service mosquitto start
Redirecting to /bin/systemctl start mosquitto.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
# sudo service mosquitto start
Redirecting to /bin/systemctl start mosquitto.service
# sudo service mosquitto status
Redirecting to /bin/systemctl status mosquitto.service
● mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2023-03-06 16:59:16 CST; 5s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Main PID: 32450 (mosquitto)
Tasks: 1
Memory: 856.0K
CGroup: /system.slice/mosquitto.service
└─32450 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Mar 06 16:59:16 hostname systemd[1]: Starting Mosquitto MQTT v3.1/v3.1.1 Broker...
Mar 06 16:59:16 hostname mosquitto[32450]: 1678093156: mosquitto version 1.6.10 starting
Mar 06 16:59:16 hostname mosquitto[32450]: 1678093156: Config loaded from /etc/mosquitto/mosquitto.conf.
Mar 06 16:59:16 hostname mosquitto[32450]: 1678093156: Opening ipv4 listen socket on port 1883.
Mar 06 16:59:16 hostname mosquitto[32450]: 1678093156: Opening ipv6 listen socket on port 1883.
Mar 06 16:59:16 hostname systemd[1]: Started Mosquitto MQTT v3.1/v3.1.1 Broker.
验证是否可用
打开两个终端,模拟发送端与接收端,其中,-t 指定主题 -m 指定内容
a.接收端
# mosquitto_sub -h 10.19.49.27 -t "mostt" -v
b.发送端
# mosquitto_pub -h 10.15.49.27 -t "mostt" -m "hello word"
# mosquitto_pub -h 10.15.49.27 -t "mostt" -m "hello word"
# mosquitto_pub -h 10.15.49.27 -t "mostt" -m "hello word3"
# mosquitto_pub -h 10.15.49.27 -t "mostt" -m "hello word3"
# mosquitto_pub -h 10.15.49.27 -t "mostt" -m "hello word4"
# mosquitto_pub -h 10.15.49.27 -t "01topic" -m "mosquitto test01"
# mosquitto_pub -h 10.15.49.27 -t "01topic" -m "mosquitto test02"
# mosquitto_pub -h 10.15.49.27 -t "01topic" -m "mosquitto test03"
此时,再查看接收端,哪个主题下的哪些内容
# mosquitto_sub -h 10.15.49.27 -t "mostt" -v
mostt hello word
mostt hello word
mostt hello word3
mostt hello word3
mostt hello word4
# mosquitto_sub -h 10.15.49.27 -t "01topic" -v
01topic mosquitto test01
01topic mosquitto test02
01topic mosquitto test03
跟kafka类似,访问安全性中都可以设置访问时的用户名密码
添加如下文件内容
# sudo less /etc/mosquitto/mosquitto.conf | grep -v "#" |grep -v "^$"
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
allow_anonymous false
password_file /etc/mosquitto/pwdfile
acl_file /etc/mosquitto/acl
参数含义
# 进程号文件
pid_file /var/run/mosquitto.pid
# 是否持久化
persistence true
persistence_location /var/lib/mosquitto/
# 关闭匿名访问
allow_anonymous false
# 密码和访问控制
password_file /etc/mosquitto/pwdfile
acl_file /etc/mosquitto/acl
设置Mosquitto的交互账号
# sudo mosquitto_passwd -c /etc/mosquitto/pwdfile testuser1
Password:
Reenter password:
# sudo mosquitto_passwd /etc/mosquitto/pwdfile testuser2
Password:
Reenter password:
第一个账号需要加-c参数,因为密码文件没有生成,后面的账号不需要再加c参数,否则是重新生成密码文件并写入账号。
配置账号与发布订阅之间的关系:
vim /etc/mosquitto/acl
# cat /etc/mosquitto/acl
user testuser1
topic write usertopic
user testuser2
topic read usertopic
设置testuser1账号只能在主题usertopic上进行发布
设置testuser2账号只能在主题usertopic上进行订阅
重启服务
# sudo service mosquitto status
Redirecting to /bin/systemctl status mosquitto.service
● mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2023-03-06 16:59:16 CST; 46min ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Main PID: 32450 (mosquitto)
Tasks: 1
Memory: 872.0K
CGroup: /system.slice/mosquitto.service
└─32450 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Mar 06 17:17:46 hostname mosquitto[32450]: 1678094266: New connection from 10.15.49.27 on port 1883.
Mar 06 17:17:46 hostname mosquitto[32450]: 1678094266: New client connected from 10.15.49.27 as mosq-D0pkHhYybXF0xuGhDW (p2, c1, k60).
Mar 06 17:17:46 hostname mosquitto[32450]: 1678094266: Client mosq-D0pkHhYybXF0xuGhDW disconnected.
Mar 06 17:17:49 hostname mosquitto[32450]: 1678094269: New connection from 10.15.49.27 on port 1883.
Mar 06 17:17:49 hostname mosquitto[32450]: 1678094269: New client connected from 10.15.49.27 as mosq-IujfILsMdH7PyItQVe (p2, c1, k60).
Mar 06 17:17:49 hostname mosquitto[32450]: 1678094269: Client mosq-IujfILsMdH7PyItQVe disconnected.
Mar 06 17:17:51 hostname mosquitto[32450]: 1678094271: New connection from 10.15.49.27 on port 1883.
Mar 06 17:17:51 hostname mosquitto[32450]: 1678094271: New client connected from 10.15.49.27 as mosq-J1wdgGJ57LPjucsxIh (p2, c1, k60).
Mar 06 17:17:51 hostname mosquitto[32450]: 1678094271: Client mosq-J1wdgGJ57LPjucsxIh disconnected.
Mar 06 17:34:31 hostname mosquitto[32450]: 1678095271: Client mosq-daklhgLG1gK0voclKu disconnected.
[sk@hostname ~]$
[sk@hostname ~]$ sudo service mosquitto restart
Redirecting to /bin/systemctl restart mosquitto.service
[sk@hostname ~]$
[sk@hostname ~]$ sudo service mosquitto status
Redirecting to /bin/systemctl status mosquitto.service
● mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2023-03-06 17:45:39 CST; 4s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Main PID: 10859 (mosquitto)
Tasks: 1
Memory: 856.0K
CGroup: /system.slice/mosquitto.service
└─10859 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Mar 06 17:45:39 hostname systemd[1]: Starting Mosquitto MQTT v3.1/v3.1.1 Broker...
Mar 06 17:45:39 hostname mosquitto[10859]: 1678095939: mosquitto version 1.6.10 starting
Mar 06 17:45:39 hostname mosquitto[10859]: 1678095939: Config loaded from /etc/mosquitto/mosquitto.conf.
Mar 06 17:45:39 hostname mosquitto[10859]: 1678095939: Opening ipv4 listen socket on port 1883.
Mar 06 17:45:39 hostname mosquitto[10859]: 1678095939: Opening ipv6 listen socket on port 1883.
Mar 06 17:45:39 hostname systemd[1]: Started Mosquitto MQTT v3.1/v3.1.1 Broker.
订阅和发布主题都需要提供正确的账号和指定的主题,如果主题不是acl中指定的,则无法收到消息。需要给用户授权。
# sudo mosquitto_pub -h 10.15.49.27 -t "usertopic" -u testuser1 -P 123456 -m "secreat"
# sudo mosquitto_pub -h 10.15.49.27 -t "usertopic" -u testuser1 -P 123456 -m "secreat2323"
# sudo mosquitto_sub -h 10.15.49.27 -t "usertopic" -v -u testuser2 -P 123456
usertopic secreat
usertopic secreat2323