文章转载自 Alson
CentOS 安装步骤
-
查询
CentOS
是否已经安装 (可选,默认没有安装)[root@iZuf6bn8133scbluly1rheZ ~]# yum list installed mosquitto Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Error: No matching Packages to list
-
查询软件源中是否有
mosquitto
安装包[root@iZuf6bn8133scbluly1rheZ ~]# yum search mosquitto Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile ======================================================================== N/S matched: mosquitto ========================================================================= mosquitto-devel.x86_64 : Development files for mosquitto mosquitto.x86_64 : An Open Source MQTT v3.1/v3.1.1 Broker Name and summary matches only, use "search all" for everything.
-
查看
mosquitto
详细信息 (可选)[root@iZuf6bn8133scbluly1rheZ ~]# yum info mosquitto Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Available Packages Name : mosquitto Arch : x86_64 Version : 1.6.10 Release : 1.el7 Size : 287 k Repo : epel/x86_64 Summary : An Open Source MQTT v3.1/v3.1.1 Broker URL : http://mosquitto.org/ License : BSD Description : Mosquitto is an open source message broker that implements the MQ Telemetry : Transport protocol version 3.1 and 3.1.1 MQTT provides a lightweight method : of carrying out messaging using a publish/subscribe model. This makes it : suitable for "machine to machine" messaging such as with low power sensors : or mobile devices such as phones, embedded computers or micro-controllers : like the Arduino.
-
安装
yum install mosquitto
-
查看
mosquitto
帮助信息[root@iZuf6bn8133scbluly1rheZ ~]# mosquitto --help mosquitto version 1.6.10 mosquitto is an MQTT v3.1.1 broker. Usage: mosquitto [-c config_file] [-d] [-h] [-p port] -c : specify the broker config file. -d : put the broker into the background after starting. -h : display this help. -p : start the broker listening on the specified port. Not recommended in conjunction with the -c option. -v : verbose mode - enable all logging types. This overrides any logging options given in the config file. See http://mosquitto.org/ for more information.
Mosquitto 配置
认证方式:用户名和密码
-
添加新用户
mosquitto_passwd -c mos_users alson
mos_users
为passwordfile
,用来保存用户的username
和password
。 -
修改配置文件,不允许匿名登录
默认配置文件
/etc/mosquitto/mosquitto.conf
,将allow_anonymous
改为 falseallow_anonymous false
-
修改配置文件,修改端口
默认端口为
1883
,可以通过修改port
指定端口port 1883
-
修改配置文件,指定
passwordfile
路径password_file /root/mos_users
-
运行服务器
[root@iZuf6bn8133scbluly1rheZ ~]# mosquitto -c /etc/mosquitto/mosquitto.conf -v 1709098551: mosquitto version 1.6.10 starting 1709098551: Config loaded from /etc/mosquitto/mosquitto.conf. 1709098551: Opening ipv4 listen socket on port 1883. 1709098551: Opening ipv6 listen socket on port 1883.
Subscriber 订阅 Topic
Subscriber 使用 MQTT.fx,具体使用方法请百度。订阅的主题为 info
Publisher
在 CentOS
上新开一个终端, 使用 mosquitto_pub
发布 Topic
为 info 的消息
mosquitto_pub -t info -u alson -P 12345678 -m "this is a test message"
问题总结
-
MQTT.fx 连接不上服务器。
检查端口是否开放。对于云服务器来说,需要在安全选项里面放开端口。如果安全选项里面已经开放端口还是连接不上,继续检查是否有别的地方可以开放端口设置。(笔者使用 宝塔面板 在服务器上搭建了 WordPress 服务器,需要在 宝塔面板 的 安全 选项中也要开放对应的端口)